
<span id="basics"></span><h1><span class="yiyi-st" id="yiyi-96">基本功能</span></h1>
        <blockquote>
        <p>原文：<a href="http://pandas.pydata.org/pandas-docs/stable/basics.html">http://pandas.pydata.org/pandas-docs/stable/basics.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<p><span class="yiyi-st" id="yiyi-97">我们在这里讨论了很多pandas数据结构通用的基本功能。</span><span class="yiyi-st" id="yiyi-98">下面是如何创建一些对象，它们在上一节中的示例中使用：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2000&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>

<span class="gp">In [2]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">])</span>

<span class="gp">In [3]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span>
<span class="gp">   ...:</span>                   <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">])</span>
<span class="gp">   ...:</span> 

<span class="gp">In [4]: </span><span class="n">wp</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Panel</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="n">items</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;Item1&apos;</span><span class="p">,</span> <span class="s1">&apos;Item2&apos;</span><span class="p">],</span>
<span class="gp">   ...:</span>               <span class="n">major_axis</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2000&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">),</span>
<span class="gp">   ...:</span>               <span class="n">minor_axis</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">,</span> <span class="s1">&apos;D&apos;</span><span class="p">])</span>
<span class="gp">   ...:</span> 
</pre></div>
</div>
<div class="section" id="head-and-tail">
<span id="basics-head-tail"></span><h2><span class="yiyi-st" id="yiyi-99">Head（取头）和 Tail（取尾）</span></h2>
<p><span class="yiyi-st" id="yiyi-100">为了查看Series或DataFrame对象的小样本，请使用<a class="reference internal" href="generated/pandas.DataFrame.head.html#pandas.DataFrame.head" title="pandas.DataFrame.head"><code class="xref py py-meth docutils literal"><span class="pre">head()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.tail.html#pandas.DataFrame.tail" title="pandas.DataFrame.tail"><code class="xref py py-meth docutils literal"><span class="pre">tail()</span></code></a>方法。</span><span class="yiyi-st" id="yiyi-101">所显示的元素的默认数量为5，但您可以传递自定义数值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [5]: </span><span class="n">long_series</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span>

<span class="gp">In [6]: </span><span class="n">long_series</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gr">Out[6]: </span>
<span class="go">0   -0.305384</span>
<span class="go">1   -0.479195</span>
<span class="go">2    0.095031</span>
<span class="go">3   -0.270099</span>
<span class="go">4   -0.707140</span>
<span class="go">dtype: float64</span>

<span class="gp">In [7]: </span><span class="n">long_series</span><span class="o">.</span><span class="n">tail</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[7]: </span>
<span class="go">997    0.588446</span>
<span class="go">998    0.026465</span>
<span class="go">999   -1.728222</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="attributes-and-the-raw-ndarray-s">
<span id="basics-attrs"></span><h2><span class="yiyi-st" id="yiyi-102">属性和原始 ndarray(s)</span></h2>
<p><span class="yiyi-st" id="yiyi-103">pandas对象有一些属性，使您可以访问元数据</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-104"><strong>shape</strong>：提供对象的各维度的尺寸，与ndarray一致</span></li>
<li><span class="yiyi-st" id="yiyi-108">轴标签</span><ul>
<li><span class="yiyi-st" id="yiyi-105"><strong>Series</strong>：<em>index</em>（唯一的轴）</span></li>
<li><span class="yiyi-st" id="yiyi-106"><strong>DataFrame</strong>：<em>index</em>（行）和<em>columns</em>（列）</span></li>
<li><span class="yiyi-st" id="yiyi-107"><strong>Panel</strong>：<em>items</em>，<em>major_axis</em>和<em>minor_axis</em></span></li>
</ul>
</li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-109">注意，<strong>这些属性可以安全地赋值</strong>！</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [8]: </span><span class="n">df</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>
<span class="gr">Out[8]: </span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-01  0.187483 -1.933946  0.377312</span>
<span class="go">2000-01-02  0.734122  2.141616 -0.011225</span>

<span class="gp">In [9]: </span><span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="p">]</span>

<span class="gp">In [10]: </span><span class="n">df</span>
<span class="gr">Out[10]: </span>
<span class="go">                   a         b         c</span>
<span class="go">2000-01-01  0.187483 -1.933946  0.377312</span>
<span class="go">2000-01-02  0.734122  2.141616 -0.011225</span>
<span class="go">2000-01-03  0.048869 -1.360687 -0.479010</span>
<span class="go">2000-01-04 -0.859661 -0.231595 -0.527750</span>
<span class="go">2000-01-05 -1.296337  0.150680  0.123836</span>
<span class="go">2000-01-06  0.571764  1.555563 -0.823761</span>
<span class="go">2000-01-07  0.535420 -1.032853  1.469725</span>
<span class="go">2000-01-08  1.304124  1.449735  0.203109</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-110">为了获取数据结构中的实际数据，只需访问<strong>values</strong>属性：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [11]: </span><span class="n">s</span><span class="o">.</span><span class="n">values</span>
<span class="gr">Out[11]: </span><span class="n">array</span><span class="p">([</span> <span class="mf">0.1122</span><span class="p">,</span>  <span class="mf">0.8717</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8161</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7849</span><span class="p">,</span>  <span class="mf">1.0307</span><span class="p">])</span>

<span class="gp">In [12]: </span><span class="n">df</span><span class="o">.</span><span class="n">values</span>
<span class="gr">Out[12]: </span>
<span class="go">array([[ 0.1875, -1.9339,  0.3773],</span>
<span class="go">       [ 0.7341,  2.1416, -0.0112],</span>
<span class="go">       [ 0.0489, -1.3607, -0.479 ],</span>
<span class="go">       [-0.8597, -0.2316, -0.5278],</span>
<span class="go">       [-1.2963,  0.1507,  0.1238],</span>
<span class="go">       [ 0.5718,  1.5556, -0.8238],</span>
<span class="go">       [ 0.5354, -1.0329,  1.4697],</span>
<span class="go">       [ 1.3041,  1.4497,  0.2031]])</span>

<span class="gp">In [13]: </span><span class="n">wp</span><span class="o">.</span><span class="n">values</span>
<span class="gr">Out[13]: </span>
<span class="go">array([[[-1.032 ,  0.9698, -0.9627,  1.3821],</span>
<span class="go">        [-0.9388,  0.6691, -0.4336, -0.2736],</span>
<span class="go">        [ 0.6804, -0.3084, -0.2761, -1.8212],</span>
<span class="go">        [-1.9936, -1.9274, -2.0279,  1.625 ],</span>
<span class="go">        [ 0.5511,  3.0593,  0.4553, -0.0307]],</span>

<span class="go">       [[ 0.9357,  1.0612, -2.1079,  0.1999],</span>
<span class="go">        [ 0.3236, -0.6416, -0.5875,  0.0539],</span>
<span class="go">        [ 0.1949, -0.382 ,  0.3186,  2.0891],</span>
<span class="go">        [-0.7283, -0.0903, -0.7482,  1.3189],</span>
<span class="go">        [-2.0298,  0.7927,  0.461 , -0.5427]]])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-111">如果DataFrame或Panel包含单一类型的数据，则ndarray实际上可以原地修改，并且更改将反映在数据结构中。</span><span class="yiyi-st" id="yiyi-112">对于异构数据（例如，某些DataFrame的列不具有全部相同的dtype），情况就不是这样。</span><span class="yiyi-st" id="yiyi-113">与轴标签不同，值属性本身不能赋值。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-114">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-115">处理异构数据时，会选取所得 ndarray 的 dtype 来适配所有涉及的数据。</span><span class="yiyi-st" id="yiyi-116">例如，如果涉及字符串，结果的dtype将是object。</span><span class="yiyi-st" id="yiyi-117">如果只有浮点数和整数，所得数组的dtype将是float 。</span></p>
</div>
</div>
<div class="section" id="accelerated-operations">
<span id="basics-accelerate"></span><h2><span class="yiyi-st" id="yiyi-118">加速操作</span></h2>
<p><span class="yiyi-st" id="yiyi-119">pandas支持使用<code class="docutils literal"><span class="pre">numexpr</span></code>库（从0.11.0开始）和<code class="docutils literal"><span class="pre">bottleneck</span></code>库，来加速某些类型的二元数值和布尔运算。</span></p>
<p><span class="yiyi-st" id="yiyi-120">这些库在处理大型数据集时非常有用，并提供可观的加速。</span><span class="yiyi-st" id="yiyi-121"><code class="docutils literal"><span class="pre">numexpr</span></code>使用智能分块，缓存和多核心。</span><span class="yiyi-st" id="yiyi-122"><code class="docutils literal"><span class="pre">bottleneck</span></code>是一组专用的cython例程，处理具有<code class="docutils literal"><span class="pre">nans</span></code>的数组时，它们特别快。</span></p>
<p><span class="yiyi-st" id="yiyi-123">以下是一个示例（使用100列 x 100,000行的<code class="docutils literal"><span class="pre">DataFrames</span></code>）：</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="25%">
<col width="25%">
<col width="25%">
<col width="25%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-124">操作</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-125">0.11.0（ms）</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-126">以前的版本（ms）</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-127">与以前的比率</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-128"><code class="docutils literal"><span class="pre">df1</span> <span class="pre">&gt;</span> <span class="pre">df2</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-129">13.32</span></td>
<td><span class="yiyi-st" id="yiyi-130">125.35</span></td>
<td><span class="yiyi-st" id="yiyi-131">0.1063</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-132"><code class="docutils literal"><span class="pre">df1</span> <span class="pre">*</span> <span class="pre">df2</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-133">21.71</span></td>
<td><span class="yiyi-st" id="yiyi-134">36.63</span></td>
<td><span class="yiyi-st" id="yiyi-135">0.5928</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-136"><code class="docutils literal"><span class="pre">df1</span> <span class="pre">+</span> <span class="pre">df2</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-137">22.04</span></td>
<td><span class="yiyi-st" id="yiyi-138">36.50</span></td>
<td><span class="yiyi-st" id="yiyi-139">0.6039</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-140">强烈建议您安装这两个库。</span><span class="yiyi-st" id="yiyi-141">更多安装信息请参阅<a class="reference internal" href="install.html#install-recommended-dependencies"><span class="std std-ref">推荐的依赖项</span></a>一节。</span></p>
</div>
<div class="section" id="flexible-binary-operations">
<span id="basics-binop"></span><h2><span class="yiyi-st" id="yiyi-142">灵活的二元运算</span></h2>
<p><span class="yiyi-st" id="yiyi-143">使用pandas数据结构的二元运算，有两个要点：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-144">高维（例如DataFrame）和低维（例如系列）对象之间的广播行为。</span></li>
<li><span class="yiyi-st" id="yiyi-145">计算中的缺失数据</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-146">我们将演示如何独立处理这些问题，虽然他们可以同时处理。</span></p>
<div class="section" id="matching-broadcasting-behavior">
<h3><span class="yiyi-st" id="yiyi-147">匹配 / 广播行为</span></h3>
<p><span class="yiyi-st" id="yiyi-148">DataFrame具有<a class="reference internal" href="generated/pandas.DataFrame.add.html#pandas.DataFrame.add" title="pandas.DataFrame.add"><code class="xref py py-meth docutils literal"><span class="pre">add()</span></code></a>，<a class="reference internal" href="generated/pandas.DataFrame.sub.html#pandas.DataFrame.sub" title="pandas.DataFrame.sub"><code class="xref py py-meth docutils literal"><span class="pre">sub()</span></code></a>，<a class="reference internal" href="generated/pandas.DataFrame.mul.html#pandas.DataFrame.mul" title="pandas.DataFrame.mul"><code class="xref py py-meth docutils literal"><span class="pre">mul()</span></code></a>，<a class="reference internal" href="generated/pandas.DataFrame.div.html#pandas.DataFrame.div" title="pandas.DataFrame.div"><code class="xref py py-meth docutils literal"><span class="pre">div()</span></code></a>方法，和<a class="reference internal" href="generated/pandas.DataFrame.radd.html#pandas.DataFrame.radd" title="pandas.DataFrame.radd"><code class="xref py py-meth docutils literal"><span class="pre">radd()</span></code></a>，<a class="reference internal" href="generated/pandas.DataFrame.rsub.html#pandas.DataFrame.rsub" title="pandas.DataFrame.rsub"><code class="xref py py-meth docutils literal"><span class="pre">rsub()</span></code></a>，...关系函数，用于执行二元操作。</span><span class="yiyi-st" id="yiyi-149">对于广播行为，系列输入是主要兴趣。</span><span class="yiyi-st" id="yiyi-150">使用这些函数，您可以通过<strong>axis</strong>关键字，匹配<em>index</em>或<em>column</em>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;one&apos;</span> <span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">]),</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;two&apos;</span> <span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">]),</span>
<span class="gp">   ....:</span>                    <span class="s1">&apos;three&apos;</span> <span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">])})</span>
<span class="gp">   ....:</span> 

<span class="gp">In [15]: </span><span class="n">df</span>
<span class="gr">Out[15]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544       NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>
<span class="go">d       NaN  1.124472 -1.101558</span>

<span class="gp">In [16]: </span><span class="n">row</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>

<span class="gp">In [17]: </span><span class="n">column</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&apos;two&apos;</span><span class="p">]</span>

<span class="gp">In [18]: </span><span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">&apos;columns&apos;</span><span class="p">)</span>
<span class="gr">Out[18]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.487650       NaN -1.487837</span>
<span class="go">b  0.000000  0.000000  0.000000</span>
<span class="go">c  0.150512  0.639504 -1.585038</span>
<span class="go">d       NaN  1.301762 -2.237808</span>

<span class="gp">In [19]: </span><span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[19]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.487650       NaN -1.487837</span>
<span class="go">b  0.000000  0.000000  0.000000</span>
<span class="go">c  0.150512  0.639504 -1.585038</span>
<span class="go">d       NaN  1.301762 -2.237808</span>

<span class="gp">In [20]: </span><span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">&apos;index&apos;</span><span class="p">)</span>
<span class="gr">Out[20]: </span>
<span class="go">        one     three  two</span>
<span class="go">a -0.274957       NaN  0.0</span>
<span class="go">b -1.275144 -1.313539  0.0</span>
<span class="go">c  0.460406  0.911003  0.0</span>
<span class="go">d       NaN  2.226031  0.0</span>

<span class="gp">In [21]: </span><span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[21]: </span>
<span class="go">        one     three  two</span>
<span class="go">a -0.274957       NaN  0.0</span>
<span class="go">b -1.275144 -1.313539  0.0</span>
<span class="go">c  0.460406  0.911003  0.0</span>
<span class="go">d       NaN  2.226031  0.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-151">此外，您可以将多索引DataFrame的某个层级与 Series对齐。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [22]: </span><span class="n">dfmi</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [23]: </span><span class="n">dfmi</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_tuples</span><span class="p">([(</span><span class="mi">1</span><span class="p">,</span><span class="s1">&apos;a&apos;</span><span class="p">),(</span><span class="mi">1</span><span class="p">,</span><span class="s1">&apos;b&apos;</span><span class="p">),(</span><span class="mi">1</span><span class="p">,</span><span class="s1">&apos;c&apos;</span><span class="p">),(</span><span class="mi">2</span><span class="p">,</span><span class="s1">&apos;a&apos;</span><span class="p">)],</span>
<span class="gp">   ....:</span>                                        <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;first&apos;</span><span class="p">,</span><span class="s1">&apos;second&apos;</span><span class="p">])</span>
<span class="gp">   ....:</span> 

<span class="gp">In [24]: </span><span class="n">dfmi</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="s1">&apos;second&apos;</span><span class="p">)</span>
<span class="gr">Out[24]: </span>
<span class="go">                   one     three       two</span>
<span class="go">first second                              </span>
<span class="go">1     a      -0.274957       NaN  0.000000</span>
<span class="go">      b      -1.275144 -1.313539  0.000000</span>
<span class="go">      c       0.460406  0.911003  0.000000</span>
<span class="go">2     a            NaN  1.476060 -0.749971</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-152">对于 Panel，描述匹配行为有点困难，因此改为使用算术方法（也许令人困惑？）</span><span class="yiyi-st" id="yiyi-153">您可以选择指定<em>广播轴</em>。</span><span class="yiyi-st" id="yiyi-154">例如，假设我们希望从数据中减去特定轴上的均值。</span><span class="yiyi-st" id="yiyi-155">这可以通过在一个轴上取平均值并在同一轴上广播来实现：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="n">major_mean</span> <span class="o">=</span> <span class="n">wp</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="s1">&apos;major&apos;</span><span class="p">)</span>

<span class="gp">In [26]: </span><span class="n">major_mean</span>
<span class="gr">Out[26]: </span>
<span class="go">      Item1     Item2</span>
<span class="go">A -0.546569 -0.260774</span>
<span class="go">B  0.492478  0.147993</span>
<span class="go">C -0.649010 -0.532794</span>
<span class="go">D  0.176307  0.623812</span>

<span class="gp">In [27]: </span><span class="n">wp</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">major_mean</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">&apos;major&apos;</span><span class="p">)</span>
<span class="gr">Out[27]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 2 (items) x 5 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: Item1 to Item2</span>
<span class="go">Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-156">对于<code class="docutils literal"><span class="pre">axis=&quot;items&quot;</span></code>和<code class="docutils literal"><span class="pre">axis=&quot;minor&quot;</span></code>也是如此。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-157">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-158">我可以确保，使DataFrame方法中的<strong>axis</strong>参数符合Panel的广播行为。</span><span class="yiyi-st" id="yiyi-159">虽然它需要一个过渡期，以便用户可以更改他们的代码...</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-160">Series 和 Index 也支持内建的<a class="reference external" href="https://docs.python.org/3/library/functions.html#divmod" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">divmod()</span></code></a>。</span><span class="yiyi-st" id="yiyi-161">该函数同时执行取底除法和模运算，同时返回一个二元组，类型与左边相同。</span><span class="yiyi-st" id="yiyi-162">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [28]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>

<span class="gp">In [29]: </span><span class="n">s</span>
<span class="gr">Out[29]: </span>
<span class="go">0    0</span>
<span class="go">1    1</span>
<span class="go">2    2</span>
<span class="go">3    3</span>
<span class="go">4    4</span>
<span class="go">5    5</span>
<span class="go">6    6</span>
<span class="go">7    7</span>
<span class="go">8    8</span>
<span class="go">9    9</span>
<span class="go">dtype: int64</span>

<span class="gp">In [30]: </span><span class="n">div</span><span class="p">,</span> <span class="n">rem</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>

<span class="gp">In [31]: </span><span class="n">div</span>
<span class="gr">Out[31]: </span>
<span class="go">0    0</span>
<span class="go">1    0</span>
<span class="go">2    0</span>
<span class="go">3    1</span>
<span class="go">4    1</span>
<span class="go">5    1</span>
<span class="go">6    2</span>
<span class="go">7    2</span>
<span class="go">8    2</span>
<span class="go">9    3</span>
<span class="go">dtype: int64</span>

<span class="gp">In [32]: </span><span class="n">rem</span>
<span class="gr">Out[32]: </span>
<span class="go">0    0</span>
<span class="go">1    1</span>
<span class="go">2    2</span>
<span class="go">3    0</span>
<span class="go">4    1</span>
<span class="go">5    2</span>
<span class="go">6    0</span>
<span class="go">7    1</span>
<span class="go">8    2</span>
<span class="go">9    0</span>
<span class="go">dtype: int64</span>

<span class="gp">In [33]: </span><span class="n">idx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>

<span class="gp">In [34]: </span><span class="n">idx</span>
<span class="gr">Out[34]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>

<span class="gp">In [35]: </span><span class="n">div</span><span class="p">,</span> <span class="n">rem</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">idx</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>

<span class="gp">In [36]: </span><span class="n">div</span>
<span class="gr">Out[36]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>

<span class="gp">In [37]: </span><span class="n">rem</span>
<span class="gr">Out[37]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-163"><a class="reference external" href="https://docs.python.org/3/library/functions.html#divmod" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">divmod()</span></code></a>也可以逐元素操作：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [38]: </span><span class="n">div</span><span class="p">,</span> <span class="n">rem</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">6</span><span class="p">])</span>

<span class="gp">In [39]: </span><span class="n">div</span>
<span class="gr">Out[39]: </span>
<span class="go">0    0</span>
<span class="go">1    0</span>
<span class="go">2    0</span>
<span class="go">3    1</span>
<span class="go">4    1</span>
<span class="go">5    1</span>
<span class="go">6    1</span>
<span class="go">7    1</span>
<span class="go">8    1</span>
<span class="go">9    1</span>
<span class="go">dtype: int64</span>

<span class="gp">In [40]: </span><span class="n">rem</span>
<span class="gr">Out[40]: </span>
<span class="go">0    0</span>
<span class="go">1    1</span>
<span class="go">2    2</span>
<span class="go">3    0</span>
<span class="go">4    0</span>
<span class="go">5    1</span>
<span class="go">6    1</span>
<span class="go">7    2</span>
<span class="go">8    2</span>
<span class="go">9    3</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
</div>
<div class="section" id="missing-data-operations-with-fill-values">
<h3><span class="yiyi-st" id="yiyi-164">缺失数据 / 填充值的操作</span></h3>
<p><span class="yiyi-st" id="yiyi-165">在Series和DataFrame中（Panel不支持），算术函数拥有输入<em>fill_value</em>的选项，当某个位置上最多缺少一个值时，它是一个用于替换的值，</span><span class="yiyi-st" id="yiyi-166">例如，将两个DataFrame对象相加时，您可能希望将NaN视为0，除非两个DataFrames都缺少该值，在这种情况下，结果将是NaN（您可以稍后使用<code class="docutils literal"><span class="pre">fillna</span></code>）。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [41]: </span><span class="n">df</span>
<span class="gr">Out[41]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544       NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>
<span class="go">d       NaN  1.124472 -1.101558</span>

<span class="gp">In [42]: </span><span class="n">df2</span>
<span class="gr">Out[42]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544  1.000000 -0.351587</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>
<span class="go">d       NaN  1.124472 -1.101558</span>

<span class="gp">In [43]: </span><span class="n">df</span> <span class="o">+</span> <span class="n">df2</span>
<span class="gr">Out[43]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -1.253088       NaN -0.703174</span>
<span class="go">b -0.277789 -0.354579  2.272499</span>
<span class="go">c  0.023235  0.924429 -0.897577</span>
<span class="go">d       NaN  2.248945 -2.203116</span>

<span class="gp">In [44]: </span><span class="n">df</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span> <span class="n">fill_value</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[44]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -1.253088  1.000000 -0.703174</span>
<span class="go">b -0.277789 -0.354579  2.272499</span>
<span class="go">c  0.023235  0.924429 -0.897577</span>
<span class="go">d       NaN  2.248945 -2.203116</span>
</pre></div>
</div>
</div>
<div class="section" id="flexible-comparisons">
<span id="basics-compare"></span><h3><span class="yiyi-st" id="yiyi-167">灵活的比较</span></h3>
<p><span class="yiyi-st" id="yiyi-168">从v0.8开始，pandas将二元比较方法eq，ne，lt，gt，le和ge引入Series和DataFrame，其行为类似于上述二元算术运算：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [45]: </span><span class="n">df</span><span class="o">.</span><span class="n">gt</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="gr">Out[45]: </span>
<span class="go">     one  three    two</span>
<span class="go">a  False  False  False</span>
<span class="go">b  False  False  False</span>
<span class="go">c  False  False  False</span>
<span class="go">d  False  False  False</span>

<span class="gp">In [46]: </span><span class="n">df2</span><span class="o">.</span><span class="n">ne</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="gr">Out[46]: </span>
<span class="go">     one  three    two</span>
<span class="go">a  False   True  False</span>
<span class="go">b  False  False  False</span>
<span class="go">c  False  False  False</span>
<span class="go">d   True  False  False</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-169">这些操作产生dtype为<code class="docutils literal"><span class="pre">bool</span></code>的pandas对象。</span><span class="yiyi-st" id="yiyi-170">这些<code class="docutils literal"><span class="pre">布尔</span></code>对象可用于索引操作，请参阅<a class="reference internal" href="indexing.html#indexing-boolean"><span class="std std-ref">这里</span></a></span></p>
</div>
<div class="section" id="boolean-reductions">
<span id="basics-reductions"></span><h3><span class="yiyi-st" id="yiyi-171">布尔归约</span></h3>
<p><span class="yiyi-st" id="yiyi-172">您可以应用归约：<a class="reference internal" href="generated/pandas.DataFrame.empty.html#pandas.DataFrame.empty" title="pandas.DataFrame.empty"><code class="xref py py-attr docutils literal"><span class="pre">empty</span></code></a>，<a class="reference internal" href="generated/pandas.DataFrame.any.html#pandas.DataFrame.any" title="pandas.DataFrame.any"><code class="xref py py-meth docutils literal"><span class="pre">any()</span></code></a>，<a class="reference internal" href="generated/pandas.DataFrame.all.html#pandas.DataFrame.all" title="pandas.DataFrame.all"><code class="xref py py-meth docutils literal"><span class="pre">all()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.bool.html#pandas.DataFrame.bool" title="pandas.DataFrame.bool"><code class="xref py py-meth docutils literal"><span class="pre">bool()</span></code></a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [47]: </span><span class="p">(</span><span class="n">df</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="gr">Out[47]: </span>
<span class="go">one      False</span>
<span class="go">three    False</span>
<span class="go">two      False</span>
<span class="go">dtype: bool</span>

<span class="gp">In [48]: </span><span class="p">(</span><span class="n">df</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">any</span><span class="p">()</span>
<span class="gr">Out[48]: </span>
<span class="go">one      True</span>
<span class="go">three    True</span>
<span class="go">two      True</span>
<span class="go">dtype: bool</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-173">您可以最终归约为布尔值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [49]: </span><span class="p">(</span><span class="n">df</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">any</span><span class="p">()</span><span class="o">.</span><span class="n">any</span><span class="p">()</span>
<span class="gr">Out[49]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-174">您可以通过<a class="reference internal" href="generated/pandas.DataFrame.empty.html#pandas.DataFrame.empty" title="pandas.DataFrame.empty"><code class="xref py py-attr docutils literal"><span class="pre">empty</span></code></a>属性测试一个pandas对象是否为空。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [50]: </span><span class="n">df</span><span class="o">.</span><span class="n">empty</span>
<span class="gr">Out[50]: </span><span class="bp">False</span>

<span class="gp">In [51]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;ABC&apos;</span><span class="p">))</span><span class="o">.</span><span class="n">empty</span>
<span class="gr">Out[51]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-175">为了在布尔上下文中求解单元素的pandas对象，请使用方法<a class="reference internal" href="generated/pandas.DataFrame.bool.html#pandas.DataFrame.bool" title="pandas.DataFrame.bool"><code class="xref py py-meth docutils literal"><span class="pre">bool()</span></code></a>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [52]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="bp">True</span><span class="p">])</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span>
<span class="gr">Out[52]: </span><span class="bp">True</span>

<span class="gp">In [53]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="bp">False</span><span class="p">])</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span>
<span class="gr">Out[53]: </span><span class="bp">False</span>

<span class="gp">In [54]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([[</span><span class="bp">True</span><span class="p">]])</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span>
<span class="gr">Out[54]: </span><span class="bp">True</span>

<span class="gp">In [55]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([[</span><span class="bp">False</span><span class="p">]])</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span>
<span class="gr">Out[55]: </span><span class="bp">False</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-176">警告</span></p>
<p><span class="yiyi-st" id="yiyi-177">您可能会试图执行以下操作：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">if</span> <span class="n">df</span><span class="p">:</span>
<span class="go">     ...</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-178">或者</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">df</span> <span class="ow">and</span> <span class="n">df2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-179">上面两个操作都会导致下面的错误</span></p>
<div class="last highlight-python"><div class="highlight"><pre><span></span><span class="ne">ValueError</span><span class="p">:</span> <span class="n">The</span> <span class="n">truth</span> <span class="n">value</span> <span class="n">of</span> <span class="n">an</span> <span class="n">array</span> <span class="ow">is</span> <span class="n">ambiguous</span><span class="o">.</span> <span class="n">Use</span> <span class="n">a</span><span class="o">.</span><span class="n">empty</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">any</span><span class="p">()</span> <span class="ow">or</span> <span class="n">a</span><span class="o">.</span><span class="n">all</span><span class="p">()</span><span class="o">.</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-180">详细讨论请参阅<a class="reference internal" href="gotchas.html#gotchas-truth"><span class="std std-ref">陷阱</span></a>。</span></p>
</div>
<div class="section" id="comparing-if-objects-are-equivalent">
<span id="basics-equals"></span><h3><span class="yiyi-st" id="yiyi-181">比较对象是否相等</span></h3>
<p><span class="yiyi-st" id="yiyi-182">通常，您可能会发现有多种方法来计算相同的结果。</span><span class="yiyi-st" id="yiyi-183">作为一个简单的例子，考虑<code class="docutils literal"><span class="pre">df+df</span></code>和<code class="docutils literal"><span class="pre">df*2</span></code>。</span><span class="yiyi-st" id="yiyi-184">为了测试这两个计算产生相同的结果，给定上面所示的工具，你可以想象使用<code class="docutils literal"><span class="pre">(df + df</span> <span class="pre">==</span> <span class="pre">df * 2).all()</span></code>。</span><span class="yiyi-st" id="yiyi-185">但实际上，这个表达式是False：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [56]: </span><span class="n">df</span><span class="o">+</span><span class="n">df</span> <span class="o">==</span> <span class="n">df</span><span class="o">*</span><span class="mi">2</span>
<span class="gr">Out[56]: </span>
<span class="go">     one  three   two</span>
<span class="go">a   True  False  True</span>
<span class="go">b   True   True  True</span>
<span class="go">c   True   True  True</span>
<span class="go">d  False   True  True</span>

<span class="gp">In [57]: </span><span class="p">(</span><span class="n">df</span><span class="o">+</span><span class="n">df</span> <span class="o">==</span> <span class="n">df</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="gr">Out[57]: </span>
<span class="go">one      False</span>
<span class="go">three    False</span>
<span class="go">two       True</span>
<span class="go">dtype: bool</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-186">请注意，布尔值的 DataFrame <code class="docutils literal"><span class="pre">df + df</span> <span class="pre">==</span> <span class="pre">df * 2</span></code>包含一些False值！</span><span class="yiyi-st" id="yiyi-187">这是因为NaN不等于自身：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [58]: </span><span class="n">np</span><span class="o">.</span><span class="n">nan</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="gr">Out[58]: </span><span class="bp">False</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-188">因此，从v0.13.1开始，NDFrames（如Series，DataFrames和Panels）拥有用于测试等性的<a class="reference internal" href="generated/pandas.DataFrame.equals.html#pandas.DataFrame.equals" title="pandas.DataFrame.equals"><code class="xref py py-meth docutils literal"><span class="pre">equals()</span></code></a>方法，其中相应位置的NaN被视为相等。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [59]: </span><span class="p">(</span><span class="n">df</span><span class="o">+</span><span class="n">df</span><span class="p">)</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="n">df</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[59]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-189">请注意，Series或DataFrame索引需要按相同的顺序，才能等于True：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [60]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;col&apos;</span><span class="p">:[</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]})</span>

<span class="gp">In [61]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;col&apos;</span><span class="p">:[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&apos;foo&apos;</span><span class="p">]},</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>

<span class="gp">In [62]: </span><span class="n">df1</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="gr">Out[62]: </span><span class="bp">False</span>

<span class="gp">In [63]: </span><span class="n">df1</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="n">df2</span><span class="o">.</span><span class="n">sort_index</span><span class="p">())</span>
<span class="gr">Out[63]: </span><span class="bp">True</span>
</pre></div>
</div>
</div>
<div class="section" id="comparing-array-like-objects">
<h3><span class="yiyi-st" id="yiyi-190">比较类似于数组的对象</span></h3>
<p><span class="yiyi-st" id="yiyi-191">将pandas数据结构与标量值进行比较时，进行逐个元素的比较会很方便：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [64]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">])</span> <span class="o">==</span> <span class="s1">&apos;foo&apos;</span>
<span class="gr">Out[64]: </span>
<span class="go">0     True</span>
<span class="go">1    False</span>
<span class="go">2    False</span>
<span class="go">dtype: bool</span>

<span class="gp">In [65]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">])</span> <span class="o">==</span> <span class="s1">&apos;foo&apos;</span>
<span class="gr">Out[65]: </span><span class="n">array</span><span class="p">([</span> <span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">False</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">bool</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-192">Pandas还处理相同长度的不同类数组的对象之间的，逐元素的比较：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [66]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">])</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;qux&apos;</span><span class="p">])</span>
<span class="gr">Out[66]: </span>
<span class="go">0     True</span>
<span class="go">1     True</span>
<span class="go">2    False</span>
<span class="go">dtype: bool</span>

<span class="gp">In [67]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">])</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;qux&apos;</span><span class="p">])</span>
<span class="gr">Out[67]: </span>
<span class="go">0     True</span>
<span class="go">1     True</span>
<span class="go">2    False</span>
<span class="go">dtype: bool</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-193">尝试比较不同长度的<code class="docutils literal"><span class="pre">Index</span></code>或<code class="docutils literal"><span class="pre">Series</span></code>对象。将产生一个ValueError：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [55]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">])</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">])</span>
<span class="go">ValueError: Series lengths must match to compare</span>

<span class="gp">In [56]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;bar&apos;</span><span class="p">,</span> <span class="s1">&apos;baz&apos;</span><span class="p">])</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;foo&apos;</span><span class="p">])</span>
<span class="go">ValueError: Series lengths must match to compare</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-194">请注意，这不同于比较可被广播的numpy行为：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [68]: </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">])</span>
<span class="gr">Out[68]: </span><span class="n">array</span><span class="p">([</span><span class="bp">False</span><span class="p">,</span>  <span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">bool</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-195">或者如果广播不能完成，它可以返回False：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [69]: </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
<span class="gr">Out[69]: </span><span class="bp">False</span>
</pre></div>
</div>
</div>
<div class="section" id="combining-overlapping-data-sets">
<h3><span class="yiyi-st" id="yiyi-196">组合重叠的数据集</span></h3>
<p><span class="yiyi-st" id="yiyi-197">当拥有两个DataFrame，其中一个的数据质量优于另一个时，那么这两个DataFrame的组合可能会有些问题。</span><span class="yiyi-st" id="yiyi-198">一个示例如下：两个表示特定经济指标的 Series，其中一个具有“更高的质量”。</span><span class="yiyi-st" id="yiyi-199">然而，较低质量的 Series 可能有更久的历史，或覆盖更完整的数据。</span><span class="yiyi-st" id="yiyi-200">因此，我们希望组合两个DataFrame对象，其中一个DataFrame中的缺失值按照条件，填充为来自其他DataFrame的类似标签的值。</span><span class="yiyi-st" id="yiyi-201">实现此操作的函数是<a class="reference internal" href="generated/pandas.DataFrame.combine_first.html#pandas.DataFrame.combine_first" title="pandas.DataFrame.combine_first"><code class="xref py py-meth docutils literal"><span class="pre">combine_first()</span></code></a>，就像这样：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [70]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mf">1.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="mf">5.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span>
<span class="gp">   ....:</span>                     <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">2.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">6.</span><span class="p">]})</span>
<span class="gp">   ....:</span> 

<span class="gp">In [71]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;A&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="mf">5.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">,</span> <span class="mf">4.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="mf">7.</span><span class="p">],</span>
<span class="gp">   ....:</span>                     <span class="s1">&apos;B&apos;</span> <span class="p">:</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="mf">4.</span><span class="p">,</span> <span class="mf">6.</span><span class="p">,</span> <span class="mf">8.</span><span class="p">]})</span>
<span class="gp">   ....:</span> 

<span class="gp">In [72]: </span><span class="n">df1</span>
<span class="gr">Out[72]: </span>
<span class="go">     A    B</span>
<span class="go">0  1.0  NaN</span>
<span class="go">1  NaN  2.0</span>
<span class="go">2  3.0  3.0</span>
<span class="go">3  5.0  NaN</span>
<span class="go">4  NaN  6.0</span>

<span class="gp">In [73]: </span><span class="n">df2</span>
<span class="gr">Out[73]: </span>
<span class="go">     A    B</span>
<span class="go">0  5.0  NaN</span>
<span class="go">1  2.0  NaN</span>
<span class="go">2  4.0  3.0</span>
<span class="go">3  NaN  4.0</span>
<span class="go">4  3.0  6.0</span>
<span class="go">5  7.0  8.0</span>

<span class="gp">In [74]: </span><span class="n">df1</span><span class="o">.</span><span class="n">combine_first</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="gr">Out[74]: </span>
<span class="go">     A    B</span>
<span class="go">0  1.0  NaN</span>
<span class="go">1  2.0  2.0</span>
<span class="go">2  3.0  3.0</span>
<span class="go">3  5.0  4.0</span>
<span class="go">4  3.0  6.0</span>
<span class="go">5  7.0  8.0</span>
</pre></div>
</div>
</div>
<div class="section" id="general-dataframe-combine">
<h3><span class="yiyi-st" id="yiyi-202">DataFrame 的通用组合</span></h3>
<p><span class="yiyi-st" id="yiyi-203">上面的<a class="reference internal" href="generated/pandas.DataFrame.combine_first.html#pandas.DataFrame.combine_first" title="pandas.DataFrame.combine_first"><code class="xref py py-meth docutils literal"><span class="pre">combine_first()</span></code></a>方法调用更通用的DataFrame方法<a class="reference internal" href="generated/pandas.DataFrame.combine.html#pandas.DataFrame.combine" title="pandas.DataFrame.combine"><code class="xref py py-meth docutils literal"><span class="pre">combine()</span></code></a>。</span><span class="yiyi-st" id="yiyi-204">此方法接受另一个DataFrame和组合器函数，对齐输入的DataFrame，然后向 Series
 偶对（即名称相同的列）传递组合器函数。</span></p>
<p><span class="yiyi-st" id="yiyi-205">因此，例如，像上面一样再次实现<a class="reference internal" href="generated/pandas.DataFrame.combine_first.html#pandas.DataFrame.combine_first" title="pandas.DataFrame.combine_first"><code class="xref py py-meth docutils literal"><span class="pre">combine_first()</span></code></a>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [75]: </span><span class="n">combiner</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">isnull</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>

<span class="gp">In [76]: </span><span class="n">df1</span><span class="o">.</span><span class="n">combine</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span> <span class="n">combiner</span><span class="p">)</span>
<span class="gr">Out[76]: </span>
<span class="go">     A    B</span>
<span class="go">0  1.0  NaN</span>
<span class="go">1  2.0  2.0</span>
<span class="go">2  3.0  3.0</span>
<span class="go">3  5.0  4.0</span>
<span class="go">4  3.0  6.0</span>
<span class="go">5  7.0  8.0</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="descriptive-statistics">
<span id="basics-stats"></span><h2><span class="yiyi-st" id="yiyi-206">描述性统计量</span></h2>
<p><span class="yiyi-st" id="yiyi-207">大量方法用于计算<a class="reference internal" href="api.html#api-series-stats"><span class="std std-ref">Series</span></a>，<a class="reference internal" href="api.html#api-dataframe-stats"><span class="std std-ref">DataFrame</span></a>和<a class="reference internal" href="api.html#api-panel-stats"><span class="std std-ref">Panel</span></a>上的描述性统计量和其他相关操作。</span><span class="yiyi-st" id="yiyi-208">这些中的大多数是像<a class="reference internal" href="generated/pandas.DataFrame.sum.html#pandas.DataFrame.sum" title="pandas.DataFrame.sum"><code class="xref py py-meth docutils literal"><span class="pre">sum()</span></code></a>，<a class="reference internal" href="generated/pandas.DataFrame.mean.html#pandas.DataFrame.mean" title="pandas.DataFrame.mean"><code class="xref py py-meth docutils literal"><span class="pre">mean()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.quantile.html#pandas.DataFrame.quantile" title="pandas.DataFrame.quantile"><code class="xref py py-meth docutils literal"><span class="pre">quantile()</span></code></a>的聚合操作（因此产生低维的结果），但是其中一些像<a class="reference internal" href="generated/pandas.DataFrame.cumsum.html#pandas.DataFrame.cumsum" title="pandas.DataFrame.cumsum"><code class="xref py py-meth docutils literal"><span class="pre">cumsum()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.cumprod.html#pandas.DataFrame.cumprod" title="pandas.DataFrame.cumprod"><code class="xref py py-meth docutils literal"><span class="pre">cumprod()</span></code></a>会生成相同大小的对象。</span><span class="yiyi-st" id="yiyi-209">通常，这些方法接受<strong>axis（轴）</strong>参数，就像<em>ndarray.{sum, std, ...}</em>，但轴可以通过名称或整数指定：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-210"><strong>Series</strong>：无需轴参数</span></li>
<li><span class="yiyi-st" id="yiyi-211"><strong>DataFrame</strong>：&quot;index&quot;（axis=0，默认值），&quot;columns&quot;（axis=1）</span></li>
<li><span class="yiyi-st" id="yiyi-212"><strong>Panel</strong>：&quot;items&quot;（axis=0），&quot;major&quot;（axis=1，默认），&quot;minor&quot;（axis=2）</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-213">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [77]: </span><span class="n">df</span>
<span class="gr">Out[77]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544       NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>
<span class="go">d       NaN  1.124472 -1.101558</span>

<span class="gp">In [78]: </span><span class="n">df</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[78]: </span>
<span class="go">one     -0.251274</span>
<span class="go">three    0.469799</span>
<span class="go">two     -0.191421</span>
<span class="go">dtype: float64</span>

<span class="gp">In [79]: </span><span class="n">df</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[79]: </span>
<span class="go">a   -0.489066</span>
<span class="go">b    0.273355</span>
<span class="go">c    0.008348</span>
<span class="go">d    0.011457</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-214">所有这些方法都有<code class="docutils literal"><span class="pre">skipna</span></code>选项，指示是否排除缺失的数据（默认情况下为<code class="docutils literal"><span class="pre">True</span></code>）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [80]: </span><span class="n">df</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">skipna</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[80]: </span>
<span class="go">one           NaN</span>
<span class="go">three         NaN</span>
<span class="go">two     -0.765684</span>
<span class="go">dtype: float64</span>

<span class="gp">In [81]: </span><span class="n">df</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">skipna</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[81]: </span>
<span class="go">a   -0.978131</span>
<span class="go">b    0.820066</span>
<span class="go">c    0.025044</span>
<span class="go">d    0.022914</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-215">结合广播/算术行为，可以非常简明地描述各种统计过程，如标准化（使数据均值为0，标准差为1）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [82]: </span><span class="n">ts_stand</span> <span class="o">=</span> <span class="p">(</span><span class="n">df</span> <span class="o">-</span> <span class="n">df</span><span class="o">.</span><span class="n">mean</span><span class="p">())</span> <span class="o">/</span> <span class="n">df</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>

<span class="gp">In [83]: </span><span class="n">ts_stand</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>
<span class="gr">Out[83]: </span>
<span class="go">one      1.0</span>
<span class="go">three    1.0</span>
<span class="go">two      1.0</span>
<span class="go">dtype: float64</span>

<span class="gp">In [84]: </span><span class="n">xs_stand</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">div</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

<span class="gp">In [85]: </span><span class="n">xs_stand</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[85]: </span>
<span class="go">a    1.0</span>
<span class="go">b    1.0</span>
<span class="go">c    1.0</span>
<span class="go">d    1.0</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-216">注意，像<a class="reference internal" href="generated/pandas.DataFrame.cumsum.html#pandas.DataFrame.cumsum" title="pandas.DataFrame.cumsum"><code class="xref py py-meth docutils literal"><span class="pre">cumsum()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.cumprod.html#pandas.DataFrame.cumprod" title="pandas.DataFrame.cumprod"><code class="xref py py-meth docutils literal"><span class="pre">cumprod()</span></code></a>的方法保留NA值的位置：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [86]: </span><span class="n">df</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span>
<span class="gr">Out[86]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544       NaN -0.351587</span>
<span class="go">b -0.765438 -0.177289  0.784662</span>
<span class="go">c -0.753821  0.284925  0.335874</span>
<span class="go">d       NaN  1.409398 -0.765684</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-217">这里是一个常用功能的快速参考汇总表。</span><span class="yiyi-st" id="yiyi-218">每个还接受可选的<code class="docutils literal"><span class="pre">level</span></code>参数，只有对象拥有<a class="reference internal" href="advanced.html#advanced-hierarchical"><span class="std std-ref">层次索引</span></a>时，该参数才适用。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="20%">
<col width="80%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-219">功能</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-220">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-221"><code class="docutils literal"><span class="pre">count</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-222">非空观测值数量</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-223"><code class="docutils literal"><span class="pre">sum</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-224">值的总和</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-225"><code class="docutils literal"><span class="pre">mean</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-226">值的平均值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-227"><code class="docutils literal"><span class="pre">mad</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-228">平均绝对偏差</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-229"><code class="docutils literal"><span class="pre">median</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-230">值的算术中值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-231"><code class="docutils literal"><span class="pre">min</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-232">最小值</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-233"><code class="docutils literal"><span class="pre">max</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-234">最大值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-235"><code class="docutils literal"><span class="pre">mode</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-236">模</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-237"><code class="docutils literal"><span class="pre">abs</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-238">绝对值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-239"><code class="docutils literal"><span class="pre">prod</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-240">值的乘积</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-241"><code class="docutils literal"><span class="pre">std</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-242">贝塞尔修正样本标准差</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-243"><code class="docutils literal"><span class="pre">var</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-244">无偏方差</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-245"><code class="docutils literal"><span class="pre">sem</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-246">平均值的标准误差</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-247"><code class="docutils literal"><span class="pre">skew</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-248">样品偏斜度（三阶矩）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-249"><code class="docutils literal"><span class="pre">kurt</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-250">样品峰度（四阶矩）</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-251"><code class="docutils literal"><span class="pre">quantile</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-252">样本分位数（百分位上的值）</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-253"><code class="docutils literal"><span class="pre">cumsum</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-254">累积总和</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-255"><code class="docutils literal"><span class="pre">cumprod</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-256">累积乘积</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-257"><code class="docutils literal"><span class="pre">cummax</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-258">累积最大值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-259"><code class="docutils literal"><span class="pre">cummin</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-260">累积最小值</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-261">请注意，有时一些NumPy方法（如<code class="docutils literal"><span class="pre">mean</span></code>，<code class="docutils literal"><span class="pre">std</span></code>和<code class="docutils literal"><span class="pre">sum</span></code>）将默认排除 Series 输入上的NA：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [87]: </span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">])</span>
<span class="gr">Out[87]: </span><span class="o">-</span><span class="mf">0.2512736517583951</span>

<span class="gp">In [88]: </span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="gr">Out[88]: </span><span class="n">nan</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-262"><code class="docutils literal"><span class="pre">Series</span></code>也有<a class="reference internal" href="generated/pandas.Series.nunique.html#pandas.Series.nunique" title="pandas.Series.nunique"><code class="xref py py-meth docutils literal"><span class="pre">nunique()</span></code></a>方法，它将返回唯一的非空值的数量：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [89]: </span><span class="n">series</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">500</span><span class="p">))</span>

<span class="gp">In [90]: </span><span class="n">series</span><span class="p">[</span><span class="mi">20</span><span class="p">:</span><span class="mi">500</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

<span class="gp">In [91]: </span><span class="n">series</span><span class="p">[</span><span class="mi">10</span><span class="p">:</span><span class="mi">20</span><span class="p">]</span>  <span class="o">=</span> <span class="mi">5</span>

<span class="gp">In [92]: </span><span class="n">series</span><span class="o">.</span><span class="n">nunique</span><span class="p">()</span>
<span class="gr">Out[92]: </span><span class="mi">11</span>
</pre></div>
</div>
<div class="section" id="summarizing-data-describe">
<span id="basics-describe"></span><h3><span class="yiyi-st" id="yiyi-263">数据汇总：describe（描述）</span></h3>
<p><span class="yiyi-st" id="yiyi-264">有一个方便的<a class="reference internal" href="generated/pandas.DataFrame.describe.html#pandas.DataFrame.describe" title="pandas.DataFrame.describe"><code class="xref py py-meth docutils literal"><span class="pre">describe()</span></code></a>函数，用于计算关于 DataFrame 的一个或多个
 Series 的各种汇总统计量（当然不包括NAs）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [93]: </span><span class="n">series</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span>

<span class="gp">In [94]: </span><span class="n">series</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

<span class="gp">In [95]: </span><span class="n">series</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="gr">Out[95]: </span>
<span class="go">count    500.000000</span>
<span class="go">mean      -0.039663</span>
<span class="go">std        1.069371</span>
<span class="go">min       -3.463789</span>
<span class="go">25%       -0.731101</span>
<span class="go">50%       -0.058918</span>
<span class="go">75%        0.672758</span>
<span class="go">max        3.120271</span>
<span class="go">dtype: float64</span>

<span class="gp">In [96]: </span><span class="n">frame</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">])</span>

<span class="gp">In [97]: </span><span class="n">frame</span><span class="o">.</span><span class="n">ix</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

<span class="gp">In [98]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="gr">Out[98]: </span>
<span class="go">                a           b           c           d           e</span>
<span class="go">count  500.000000  500.000000  500.000000  500.000000  500.000000</span>
<span class="go">mean     0.000954   -0.044014    0.075936   -0.003679    0.020751</span>
<span class="go">std      1.005133    0.974882    0.967432    1.004732    0.963812</span>
<span class="go">min     -3.010899   -2.782760   -3.401252   -2.944925   -3.794127</span>
<span class="go">25%     -0.682900   -0.681161   -0.528190   -0.663503   -0.615717</span>
<span class="go">50%     -0.001651   -0.006279    0.040098   -0.003378    0.006282</span>
<span class="go">75%      0.656439    0.632852    0.717919    0.687214    0.653423</span>
<span class="go">max      3.007143    2.627688    2.702490    2.850852    3.072117</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-265">您可以选择包含在输出中的特定百分位数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [99]: </span><span class="n">series</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">percentiles</span><span class="o">=</span><span class="p">[</span><span class="o">.</span><span class="mo">05</span><span class="p">,</span> <span class="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="o">.</span><span class="mi">75</span><span class="p">,</span> <span class="o">.</span><span class="mi">95</span><span class="p">])</span>
<span class="gr">Out[99]: </span>
<span class="go">count    500.000000</span>
<span class="go">mean      -0.039663</span>
<span class="go">std        1.069371</span>
<span class="go">min       -3.463789</span>
<span class="go">5%        -1.741334</span>
<span class="go">25%       -0.731101</span>
<span class="go">50%       -0.058918</span>
<span class="go">75%        0.672758</span>
<span class="go">95%        1.854383</span>
<span class="go">max        3.120271</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-266">默认情况下，始终包括中位数。</span></p>
<p><span class="yiyi-st" id="yiyi-267">对于非数值 Series 对象，<a class="reference internal" href="generated/pandas.Series.describe.html#pandas.Series.describe" title="pandas.Series.describe"><code class="xref py py-meth docutils literal"><span class="pre">describe()</span></code></a>将给出唯一值数量，和最常出现的值的简单摘要：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [100]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">])</span>

<span class="gp">In [101]: </span><span class="n">s</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="gr">Out[101]: </span>
<span class="go">count     9</span>
<span class="go">unique    4</span>
<span class="go">top       a</span>
<span class="go">freq      5</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-268">请注意，在混合类型DataFrame对象上，<a class="reference internal" href="generated/pandas.DataFrame.describe.html#pandas.DataFrame.describe" title="pandas.DataFrame.describe"><code class="xref py py-meth docutils literal"><span class="pre">describe()</span></code></a>将摘要限制为仅包含数字列，如果没有，则只包含类别列：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [102]: </span><span class="n">frame</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;Yes&apos;</span><span class="p">,</span> <span class="s1">&apos;Yes&apos;</span><span class="p">,</span> <span class="s1">&apos;No&apos;</span><span class="p">,</span> <span class="s1">&apos;No&apos;</span><span class="p">],</span> <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)})</span>

<span class="gp">In [103]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="gr">Out[103]: </span>
<span class="go">              b</span>
<span class="go">count  4.000000</span>
<span class="go">mean   1.500000</span>
<span class="go">std    1.290994</span>
<span class="go">min    0.000000</span>
<span class="go">25%    0.750000</span>
<span class="go">50%    1.500000</span>
<span class="go">75%    2.250000</span>
<span class="go">max    3.000000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-269">可以通过提供类型列表，作为<code class="docutils literal"><span class="pre">include</span></code> / <code class="docutils literal"><span class="pre">exclude</span></code>参数，来控制此行为。</span><span class="yiyi-st" id="yiyi-270">也可以使用特殊值<code class="docutils literal"><span class="pre">all</span></code>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [104]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;object&apos;</span><span class="p">])</span>
<span class="gr">Out[104]: </span>
<span class="go">         a</span>
<span class="go">count    4</span>
<span class="go">unique   2</span>
<span class="go">top     No</span>
<span class="go">freq     2</span>

<span class="gp">In [105]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;number&apos;</span><span class="p">])</span>
<span class="gr">Out[105]: </span>
<span class="go">              b</span>
<span class="go">count  4.000000</span>
<span class="go">mean   1.500000</span>
<span class="go">std    1.290994</span>
<span class="go">min    0.000000</span>
<span class="go">25%    0.750000</span>
<span class="go">50%    1.500000</span>
<span class="go">75%    2.250000</span>
<span class="go">max    3.000000</span>

<span class="gp">In [106]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="s1">&apos;all&apos;</span><span class="p">)</span>
<span class="gr">Out[106]: </span>
<span class="go">          a         b</span>
<span class="go">count     4  4.000000</span>
<span class="go">unique    2       NaN</span>
<span class="go">top      No       NaN</span>
<span class="go">freq      2       NaN</span>
<span class="go">mean    NaN  1.500000</span>
<span class="go">std     NaN  1.290994</span>
<span class="go">min     NaN  0.000000</span>
<span class="go">25%     NaN  0.750000</span>
<span class="go">50%     NaN  1.500000</span>
<span class="go">75%     NaN  2.250000</span>
<span class="go">max     NaN  3.000000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-271">该功能依赖于<a class="reference internal" href="#basics-selectdtypes"><span class="std std-ref">select_dtypes</span></a>。</span><span class="yiyi-st" id="yiyi-272">有关接受的输入的详细信息，请参阅此处。</span></p>
</div>
<div class="section" id="index-of-min-max-values">
<span id="basics-idxmin"></span><h3><span class="yiyi-st" id="yiyi-273">最大/最小值的索引</span></h3>
<p><span class="yiyi-st" id="yiyi-274">Series和DataFrame上的<a class="reference internal" href="generated/pandas.DataFrame.idxmin.html#pandas.DataFrame.idxmin" title="pandas.DataFrame.idxmin"><code class="xref py py-meth docutils literal"><span class="pre">idxmin()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.idxmax.html#pandas.DataFrame.idxmax" title="pandas.DataFrame.idxmax"><code class="xref py py-meth docutils literal"><span class="pre">idxmax()</span></code></a>函数计算最大值和最小值的索引标签：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [107]: </span><span class="n">s1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>

<span class="gp">In [108]: </span><span class="n">s1</span>
<span class="gr">Out[108]: </span>
<span class="go">0   -0.872725</span>
<span class="go">1    1.522411</span>
<span class="go">2    0.080594</span>
<span class="go">3   -1.676067</span>
<span class="go">4    0.435804</span>
<span class="go">dtype: float64</span>

<span class="gp">In [109]: </span><span class="n">s1</span><span class="o">.</span><span class="n">idxmin</span><span class="p">(),</span> <span class="n">s1</span><span class="o">.</span><span class="n">idxmax</span><span class="p">()</span>
<span class="gr">Out[109]: </span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

<span class="gp">In [110]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span><span class="s1">&apos;B&apos;</span><span class="p">,</span><span class="s1">&apos;C&apos;</span><span class="p">])</span>

<span class="gp">In [111]: </span><span class="n">df1</span>
<span class="gr">Out[111]: </span>
<span class="go">          A         B         C</span>
<span class="go">0  0.445734 -1.649461  0.169660</span>
<span class="go">1  1.246181  0.131682 -2.001988</span>
<span class="go">2 -1.273023  0.870502  0.214583</span>
<span class="go">3  0.088452 -0.173364  1.207466</span>
<span class="go">4  0.546121  0.409515 -0.310515</span>

<span class="gp">In [112]: </span><span class="n">df1</span><span class="o">.</span><span class="n">idxmin</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[112]: </span>
<span class="go">A    2</span>
<span class="go">B    0</span>
<span class="go">C    1</span>
<span class="go">dtype: int64</span>

<span class="gp">In [113]: </span><span class="n">df1</span><span class="o">.</span><span class="n">idxmax</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[113]: </span>
<span class="go">0    A</span>
<span class="go">1    A</span>
<span class="go">2    B</span>
<span class="go">3    C</span>
<span class="go">4    A</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-275">当有多个行（或列）匹配最小值或最大值时，<a class="reference internal" href="generated/pandas.DataFrame.idxmin.html#pandas.DataFrame.idxmin" title="pandas.DataFrame.idxmin"><code class="xref py py-meth docutils literal"><span class="pre">idxmin()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.idxmax.html#pandas.DataFrame.idxmax" title="pandas.DataFrame.idxmax"><code class="xref py py-meth docutils literal"><span class="pre">idxmax()</span></code></a>返回第一个匹配的索引：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [114]: </span><span class="n">df3</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">&apos;edcba&apos;</span><span class="p">))</span>

<span class="gp">In [115]: </span><span class="n">df3</span>
<span class="gr">Out[115]: </span>
<span class="go">     A</span>
<span class="go">e  2.0</span>
<span class="go">d  1.0</span>
<span class="go">c  1.0</span>
<span class="go">b  3.0</span>
<span class="go">a  NaN</span>

<span class="gp">In [116]: </span><span class="n">df3</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">idxmin</span><span class="p">()</span>
<span class="gr">Out[116]: </span><span class="s1">&apos;d&apos;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-276">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-277">在NumPy中，<code class="docutils literal"><span class="pre">idxmin</span></code>和<code class="docutils literal"><span class="pre">idxmax</span></code>称为<code class="docutils literal"><span class="pre">argmin</span></code>和<code class="docutils literal"><span class="pre">argmax</span></code>。</span></p>
</div>
</div>
<div class="section" id="value-counts-histogramming-mode">
<span id="basics-discretization"></span><h3><span class="yiyi-st" id="yiyi-278">值的计数（直方图）/模式</span></h3>
<p><span class="yiyi-st" id="yiyi-279">Series 的<a class="reference internal" href="generated/pandas.Series.value_counts.html#pandas.Series.value_counts" title="pandas.Series.value_counts"><code class="xref py py-meth docutils literal"><span class="pre">value_counts()</span></code></a>方法和同名的顶级函数计算一维数组值的直方图。</span><span class="yiyi-st" id="yiyi-280">它也可以用作常规数组上的函数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [117]: </span><span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>

<span class="gp">In [118]: </span><span class="n">data</span>
<span class="gr">Out[118]: </span>
<span class="go">array([5, 3, 2, 2, 1, 4, 0, 4, 0, 2, 0, 6, 4, 1, 6, 3, 3, 0, 2, 1, 0, 5, 5,</span>
<span class="go">       3, 6, 1, 5, 6, 2, 0, 0, 6, 3, 3, 5, 0, 4, 3, 3, 3, 0, 6, 1, 3, 5, 5,</span>
<span class="go">       0, 4, 0, 6])</span>

<span class="gp">In [119]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

<span class="gp">In [120]: </span><span class="n">s</span><span class="o">.</span><span class="n">value_counts</span><span class="p">()</span>
<span class="gr">Out[120]: </span>
<span class="go">0    11</span>
<span class="go">3    10</span>
<span class="go">6     7</span>
<span class="go">5     7</span>
<span class="go">4     5</span>
<span class="go">2     5</span>
<span class="go">1     5</span>
<span class="go">dtype: int64</span>

<span class="gp">In [121]: </span><span class="n">pd</span><span class="o">.</span><span class="n">value_counts</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="gr">Out[121]: </span>
<span class="go">0    11</span>
<span class="go">3    10</span>
<span class="go">6     7</span>
<span class="go">5     7</span>
<span class="go">4     5</span>
<span class="go">2     5</span>
<span class="go">1     5</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-281">与之类似，您可以在Series或DataFrame中。获取最频繁出现的值（模式）：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [122]: </span><span class="n">s5</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">7</span><span class="p">])</span>

<span class="gp">In [123]: </span><span class="n">s5</span><span class="o">.</span><span class="n">mode</span><span class="p">()</span>
<span class="gr">Out[123]: </span>
<span class="go">0    3</span>
<span class="go">1    7</span>
<span class="go">dtype: int64</span>

<span class="gp">In [124]: </span><span class="n">df5</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s2">&quot;A&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">50</span><span class="p">),</span>
<span class="gp">   .....:</span>                     <span class="s2">&quot;B&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">50</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [125]: </span><span class="n">df5</span><span class="o">.</span><span class="n">mode</span><span class="p">()</span>
<span class="gr">Out[125]: </span>
<span class="go">   A  B</span>
<span class="go">0  1 -5</span>
</pre></div>
</div>
</div>
<div class="section" id="discretization-and-quantiling">
<h3><span class="yiyi-st" id="yiyi-282">离散化和量化</span></h3>
<p><span class="yiyi-st" id="yiyi-283">可以使用<a class="reference internal" href="generated/pandas.cut.html#pandas.cut" title="pandas.cut"><code class="xref py py-func docutils literal"><span class="pre">cut()</span></code></a>（基于值的分桶）和<a class="reference internal" href="generated/pandas.qcut.html#pandas.qcut" title="pandas.qcut"><code class="xref py py-func docutils literal"><span class="pre">qcut()</span></code></a>（基于样本分位数的分桶）函数将连续值变离散：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [126]: </span><span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>

<span class="gp">In [127]: </span><span class="n">factor</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">cut</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>

<span class="gp">In [128]: </span><span class="n">factor</span>
<span class="gr">Out[128]: </span>
<span class="go">[(-0.645, 0.336], (-2.61, -1.626], (-1.626, -0.645], (-1.626, -0.645], (-1.626, -0.645], ..., (0.336, 1.316], (0.336, 1.316], (0.336, 1.316], (0.336, 1.316], (-2.61, -1.626]]</span>
<span class="go">Length: 20</span>
<span class="go">Categories (4, object): [(-2.61, -1.626] &lt; (-1.626, -0.645] &lt; (-0.645, 0.336] &lt; (0.336, 1.316]]</span>

<span class="gp">In [129]: </span><span class="n">factor</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">cut</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="p">[</span><span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>

<span class="gp">In [130]: </span><span class="n">factor</span>
<span class="gr">Out[130]: </span>
<span class="go">[(-1, 0], (-5, -1], (-1, 0], (-5, -1], (-1, 0], ..., (0, 1], (1, 5], (0, 1], (0, 1], (-5, -1]]</span>
<span class="go">Length: 20</span>
<span class="go">Categories (4, object): [(-5, -1] &lt; (-1, 0] &lt; (0, 1] &lt; (1, 5]]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-284"><a class="reference internal" href="generated/pandas.qcut.html#pandas.qcut" title="pandas.qcut"><code class="xref py py-func docutils literal"><span class="pre">qcut()</span></code></a>计算样本分位数。</span><span class="yiyi-st" id="yiyi-285">例如，我们可以将一些正态分布的数据分割成等大小的四部分，如下所示：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [131]: </span><span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>

<span class="gp">In [132]: </span><span class="n">factor</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">qcut</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="o">.</span><span class="mi">5</span><span class="p">,</span> <span class="o">.</span><span class="mi">75</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span>

<span class="gp">In [133]: </span><span class="n">factor</span>
<span class="gr">Out[133]: </span>
<span class="go">[(-0.139, 1.00736], (1.00736, 1.976], (1.00736, 1.976], [-1.0705, -0.439], [-1.0705, -0.439], ..., (1.00736, 1.976], [-1.0705, -0.439], (-0.439, -0.139], (-0.439, -0.139], (-0.439, -0.139]]</span>
<span class="go">Length: 30</span>
<span class="go">Categories (4, object): [[-1.0705, -0.439] &lt; (-0.439, -0.139] &lt; (-0.139, 1.00736] &lt; (1.00736, 1.976]]</span>

<span class="gp">In [134]: </span><span class="n">pd</span><span class="o">.</span><span class="n">value_counts</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span>
<span class="gr">Out[134]: </span>
<span class="go">(1.00736, 1.976]     8</span>
<span class="go">[-1.0705, -0.439]    8</span>
<span class="go">(-0.139, 1.00736]    7</span>
<span class="go">(-0.439, -0.139]     7</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-286">我们还可以传递无限值来定义bin：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [135]: </span><span class="n">arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>

<span class="gp">In [136]: </span><span class="n">factor</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">cut</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span> <span class="p">[</span><span class="o">-</span><span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">inf</span><span class="p">])</span>

<span class="gp">In [137]: </span><span class="n">factor</span>
<span class="gr">Out[137]: </span>
<span class="go">[(-inf, 0], (0, inf], (0, inf], (0, inf], (-inf, 0], ..., (-inf, 0], (0, inf], (-inf, 0], (-inf, 0], (0, inf]]</span>
<span class="go">Length: 20</span>
<span class="go">Categories (2, object): [(-inf, 0] &lt; (0, inf]]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="function-application">
<span id="basics-apply"></span><h2><span class="yiyi-st" id="yiyi-287">函数应用</span></h2>
<p><span class="yiyi-st" id="yiyi-288">要将自己的或另一个库的函数应用于pandas对象，您应该注意以下三个方法。</span><span class="yiyi-st" id="yiyi-289">使用的适当方法取决于您的函数是否希望对整个<code class="docutils literal"><span class="pre">DataFrame</span></code>还是<code class="docutils literal"><span class="pre">Series</span></code>，按行或列或者元素进行操作。</span></p>
<ol class="arabic simple">
<li><span class="yiyi-st" id="yiyi-290"><a class="reference internal" href="#tablewise-function-application">按表格的函数应用</a>：<a class="reference internal" href="generated/pandas.DataFrame.pipe.html#pandas.DataFrame.pipe" title="pandas.DataFrame.pipe"><code class="xref py py-meth docutils literal"><span class="pre">pipe()</span></code></a></span></li>
<li><span class="yiyi-st" id="yiyi-291"><a class="reference internal" href="#row-or-column-wise-function-application">按行或列的函数应用</a>：<a class="reference internal" href="generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply" title="pandas.DataFrame.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a></span></li>
<li><span class="yiyi-st" id="yiyi-292"><a class="reference internal" href="#elementwise">按元素</a>的函数应用：<a class="reference internal" href="generated/pandas.DataFrame.applymap.html#pandas.DataFrame.applymap" title="pandas.DataFrame.applymap"><code class="xref py py-meth docutils literal"><span class="pre">applymap()</span></code></a></span></li>
</ol>
<div class="section" id="tablewise-function-application">
<span id="basics-pipe"></span><h3><span class="yiyi-st" id="yiyi-293">按表格的函数应用</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-294"><span class="versionmodified">版本0.16.2中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-295"><code class="docutils literal"><span class="pre">DataFrames</span></code>和<code class="docutils literal"><span class="pre">Series</span></code>当然可以传递到函数中。</span><span class="yiyi-st" id="yiyi-296">但是，如果函数需要链式调用，请考虑使用<a class="reference internal" href="generated/pandas.DataFrame.pipe.html#pandas.DataFrame.pipe" title="pandas.DataFrame.pipe"><code class="xref py py-meth docutils literal"><span class="pre">pipe()</span></code></a>方法。</span><span class="yiyi-st" id="yiyi-297">比较以下内容</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># f, g, and h are functions taking and returning ``DataFrames``</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">f</span><span class="p">(</span><span class="n">g</span><span class="p">(</span><span class="n">h</span><span class="p">(</span><span class="n">df</span><span class="p">),</span> <span class="n">arg1</span><span class="o">=</span><span class="mi">1</span><span class="p">),</span> <span class="n">arg2</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">arg3</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-298">与等效</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">pipe</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
<span class="go">       .pipe(g, arg1=1)</span>
<span class="go">       .pipe(f, arg2=2, arg3=3)</span>
<span class="go">    )</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-299">pandas 鼓励第二种风格，这被称为方法链。</span><span class="yiyi-st" id="yiyi-300"><code class="docutils literal"><span class="pre">pipe</span></code>可以方便地在方法链中调用你自己的或另一个库的函数，以及pandas的方法。</span></p>
<p><span class="yiyi-st" id="yiyi-301">在上述示例中，函数<code class="docutils literal"><span class="pre">f</span></code>，<code class="docutils literal"><span class="pre">g</span></code>和<code class="docutils literal"><span class="pre">h</span></code>每个都期待<code class="docutils literal"><span class="pre">DataFrame</span></code>为第一个参数。</span><span class="yiyi-st" id="yiyi-302">如果你希望应用的函数把它的数据接受为第二个参数怎么办？</span><span class="yiyi-st" id="yiyi-303">在这种情况下，将元组<code class="docutils literal"><span class="pre">(callable，</span> <span class="pre">data_keyword)</span></code>提供给<code class="docutils literal"><span class="pre">pipe</span></code>。</span><span class="yiyi-st" id="yiyi-304"><code class="docutils literal"><span class="pre">.pipe</span></code>会将<code class="docutils literal"><span class="pre">DataFrame</span></code>赋给元组中指定的参数。</span></p>
<p><span class="yiyi-st" id="yiyi-305">例如，我们可以使用statsmodels拟合回归。</span><span class="yiyi-st" id="yiyi-306">他们的API首先接受公式，并且将<code class="docutils literal"><span class="pre">DataFrame</span></code>作为第二个参数，<code class="docutils literal"><span class="pre">data</span></code>。</span><span class="yiyi-st" id="yiyi-307">我们将关键字偶对<code class="docutils literal"><span class="pre">(sm.poisson, </span> <span class="pre">&apos;data&apos;)</span></code>传递给<code class="docutils literal"><span class="pre">pipe</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [138]: </span><span class="kn">import</span> <span class="nn">statsmodels.formula.api</span> <span class="kn">as</span> <span class="nn">sm</span>

<span class="gp">In [139]: </span><span class="n">bb</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&apos;data/baseball.csv&apos;</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="s1">&apos;id&apos;</span><span class="p">)</span>

<span class="gp">In [140]: </span><span class="p">(</span><span class="n">bb</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">&apos;h &gt; 0&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span>    <span class="o">.</span><span class="n">assign</span><span class="p">(</span><span class="n">ln_h</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">h</span><span class="p">))</span>
<span class="gp">   .....:</span>    <span class="o">.</span><span class="n">pipe</span><span class="p">((</span><span class="n">sm</span><span class="o">.</span><span class="n">poisson</span><span class="p">,</span> <span class="s1">&apos;data&apos;</span><span class="p">),</span> <span class="s1">&apos;hr ~ ln_h + year + g + C(lg)&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span>    <span class="o">.</span><span class="n">fit</span><span class="p">()</span>
<span class="gp">   .....:</span>    <span class="o">.</span><span class="n">summary</span><span class="p">()</span>
<span class="gp">   .....:</span> <span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">Optimization terminated successfully.</span>
<span class="go">         Current function value: 2.116284</span>
<span class="go">         Iterations 24</span>
<span class="gr">Out[140]: </span>
<span class="go">&lt;class &apos;statsmodels.iolib.summary.Summary&apos;&gt;</span>
<span class="go">&quot;&quot;&quot;</span>
<span class="go">                          Poisson Regression Results                          </span>
<span class="go">==============================================================================</span>
<span class="go">Dep. Variable:                     hr   No. Observations:                   68</span>
<span class="go">Model:                        Poisson   Df Residuals:                       63</span>
<span class="go">Method:                           MLE   Df Model:                            4</span>
<span class="go">Date:                Sat, 24 Dec 2016   Pseudo R-squ.:                  0.6878</span>
<span class="go">Time:                        18:31:33   Log-Likelihood:                -143.91</span>
<span class="go">converged:                       True   LL-Null:                       -460.91</span>
<span class="go">                                        LLR p-value:                6.774e-136</span>
<span class="go">===============================================================================</span>
<span class="go">                  coef    std err          z      P&gt;|z|      [95.0% Conf. Int.]</span>
<span class="go">-------------------------------------------------------------------------------</span>
<span class="go">Intercept   -1267.3636    457.867     -2.768      0.006     -2164.767  -369.960</span>
<span class="go">C(lg)[T.NL]    -0.2057      0.101     -2.044      0.041        -0.403    -0.008</span>
<span class="go">ln_h            0.9280      0.191      4.866      0.000         0.554     1.302</span>
<span class="go">year            0.6301      0.228      2.762      0.006         0.183     1.077</span>
<span class="go">g               0.0099      0.004      2.754      0.006         0.003     0.017</span>
<span class="go">===============================================================================</span>
<span class="go">&quot;&quot;&quot;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-308">pipe方法受unix管道，以及最近的<a class="reference external" href="https://github.com/hadley/dplyr">dplyr</a>和<a class="reference external" href="https://github.com/smbache/magrittr">magrittr</a>的启发，它们将流行的<code class="docutils literal"><span class="pre">(%&gt;%)</span></code>（管道读取）运算符引入了<a class="reference external" href="http://www.r-project.org">R</a>。 这里的<code class="docutils literal"><span class="pre">pipe</span></code>相当干净，在python 中感觉很舒服。</span><span class="yiyi-st" id="yiyi-309">我们建议您查看源代码（IPython 中执行<code class="docutils literal"><span class="pre">pd.DataFrame.pipe??</span></code>）</span><span class="yiyi-st" id="yiyi-310">。</span></p>
</div>
<div class="section" id="row-or-column-wise-function-application">
<h3><span class="yiyi-st" id="yiyi-311">逐行或逐列的函数应用</span></h3>
<p><span class="yiyi-st" id="yiyi-312">可以使用<a class="reference internal" href="generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply" title="pandas.DataFrame.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>方法，沿着DataFrame或Panel的轴应用任意函数，与描述性统计方法一样，它采用可选的<code class="docutils literal"><span class="pre">axis</span></code>参数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [141]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">)</span>
<span class="gr">Out[141]: </span>
<span class="go">one     -0.251274</span>
<span class="go">three    0.469799</span>
<span class="go">two     -0.191421</span>
<span class="go">dtype: float64</span>

<span class="gp">In [142]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[142]: </span>
<span class="go">a   -0.489066</span>
<span class="go">b    0.273355</span>
<span class="go">c    0.008348</span>
<span class="go">d    0.011457</span>
<span class="go">dtype: float64</span>

<span class="gp">In [143]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">-</span> <span class="n">x</span><span class="o">.</span><span class="n">min</span><span class="p">())</span>
<span class="gr">Out[143]: </span>
<span class="go">one      0.638161</span>
<span class="go">three    1.301762</span>
<span class="go">two      2.237808</span>
<span class="go">dtype: float64</span>

<span class="gp">In [144]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">cumsum</span><span class="p">)</span>
<span class="gr">Out[144]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544       NaN -0.351587</span>
<span class="go">b -0.765438 -0.177289  0.784662</span>
<span class="go">c -0.753821  0.284925  0.335874</span>
<span class="go">d       NaN  1.409398 -0.765684</span>

<span class="gp">In [145]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">)</span>
<span class="gr">Out[145]: </span>
<span class="go">        one     three       two</span>
<span class="go">a  0.534436       NaN  0.703570</span>
<span class="go">b  0.870320  0.837537  3.115063</span>
<span class="go">c  1.011685  1.587586  0.638401</span>
<span class="go">d       NaN  3.078592  0.332353</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-313">根据传递给<a class="reference internal" href="generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply" title="pandas.DataFrame.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>的函数的返回类型，结果将是较低维或相同维的。</span></p>
<p><span class="yiyi-st" id="yiyi-314"><a class="reference internal" href="generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply" title="pandas.DataFrame.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>结合一些机智的用法，可以用来回答关于数据集的许多问题。</span><span class="yiyi-st" id="yiyi-315">例如，假设我们要提取每一列的最大值出现处的日期：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [146]: </span><span class="n">tsdf</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                     <span class="n">index</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/1/2000&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">1000</span><span class="p">))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [147]: </span><span class="n">tsdf</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">idxmax</span><span class="p">())</span>
<span class="gr">Out[147]: </span>
<span class="go">A   2001-04-27</span>
<span class="go">B   2002-06-02</span>
<span class="go">C   2000-04-02</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-316">您还可以向<a class="reference internal" href="generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply" title="pandas.DataFrame.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>方法传递其他参数和关键字参数。</span><span class="yiyi-st" id="yiyi-317">例如，考虑以下您要应用的函数：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">subtract_and_divide</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">sub</span><span class="p">,</span> <span class="n">divide</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="k">return</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">sub</span><span class="p">)</span> <span class="o">/</span> <span class="n">divide</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-318">然后，您可以应用这个函数，如下所示：</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">subtract_and_divide</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="mi">5</span><span class="p">,),</span> <span class="n">divide</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-319">另一个有用的功能是，传递Series方法对每一列或行执行一些Series操作：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [148]: </span><span class="n">tsdf</span>
<span class="gr">Out[148]: </span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-01  1.796883 -0.930690  3.542846</span>
<span class="go">2000-01-02 -1.242888 -0.695279 -1.000884</span>
<span class="go">2000-01-03 -0.720299  0.546303 -0.082042</span>
<span class="go">2000-01-04       NaN       NaN       NaN</span>
<span class="go">2000-01-05       NaN       NaN       NaN</span>
<span class="go">2000-01-06       NaN       NaN       NaN</span>
<span class="go">2000-01-07       NaN       NaN       NaN</span>
<span class="go">2000-01-08 -0.527402  0.933507  0.129646</span>
<span class="go">2000-01-09 -0.338903 -1.265452 -1.969004</span>
<span class="go">2000-01-10  0.532566  0.341548  0.150493</span>

<span class="gp">In [149]: </span><span class="n">tsdf</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="o">.</span><span class="n">interpolate</span><span class="p">)</span>
<span class="gr">Out[149]: </span>
<span class="go">                   A         B         C</span>
<span class="go">2000-01-01  1.796883 -0.930690  3.542846</span>
<span class="go">2000-01-02 -1.242888 -0.695279 -1.000884</span>
<span class="go">2000-01-03 -0.720299  0.546303 -0.082042</span>
<span class="go">2000-01-04 -0.681720  0.623743 -0.039704</span>
<span class="go">2000-01-05 -0.643140  0.701184  0.002633</span>
<span class="go">2000-01-06 -0.604561  0.778625  0.044971</span>
<span class="go">2000-01-07 -0.565982  0.856066  0.087309</span>
<span class="go">2000-01-08 -0.527402  0.933507  0.129646</span>
<span class="go">2000-01-09 -0.338903 -1.265452 -1.969004</span>
<span class="go">2000-01-10  0.532566  0.341548  0.150493</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-320">最后，<a class="reference internal" href="generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply" title="pandas.DataFrame.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>接受参数<code class="docutils literal"><span class="pre">raw</span></code>，它默认为False，它在应用函数之前将每一行或列转换为一个Series。</span><span class="yiyi-st" id="yiyi-321">当设置为True时，传递的函数将接收一个ndarray对象，如果您不需要索引功能，这会有正面的性能影响。</span></p>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-322">另见</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-323"><a class="reference internal" href="groupby.html#groupby"><span class="std std-ref">GroupBy</span></a>章节演示了相关的灵活功能，用于按照某些标准进行分组，应用结果并将结果组合为Series，DataFrame等。</span></p>
</div>
</div>
<div class="section" id="applying-elementwise-python-functions">
<span id="elementwise"></span><h3><span class="yiyi-st" id="yiyi-324">应用逐元素的 Python 函数</span></h3>
<p><span class="yiyi-st" id="yiyi-325">由于并非所有函数都可以向量化（接受NumPy数组并返回另一个数组或值），DataFrame上的<a class="reference internal" href="generated/pandas.DataFrame.applymap.html#pandas.DataFrame.applymap" title="pandas.DataFrame.applymap"><code class="xref py py-meth docutils literal"><span class="pre">applymap()</span></code></a>方法和类似的 Series 上的<a class="reference internal" href="generated/pandas.Series.map.html#pandas.Series.map" title="pandas.Series.map"><code class="xref py py-meth docutils literal"><span class="pre">map()</span></code></a>方法，接受任何这样的 Python 函数，它接受单个参数并返回单个值。</span><span class="yiyi-st" id="yiyi-326">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [150]: </span><span class="n">df4</span>
<span class="gr">Out[150]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544       NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>
<span class="go">d       NaN  1.124472 -1.101558</span>

<span class="gp">In [151]: </span><span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>

<span class="gp">In [152]: </span><span class="n">df4</span><span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gr">Out[152]: </span>
<span class="go">a    14</span>
<span class="go">b    15</span>
<span class="go">c    15</span>
<span class="go">d     3</span>
<span class="go">Name: one, dtype: int64</span>

<span class="gp">In [153]: </span><span class="n">df4</span><span class="o">.</span><span class="n">applymap</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gr">Out[153]: </span>
<span class="go">   one  three  two</span>
<span class="go">a   14      3   15</span>
<span class="go">b   15     15   11</span>
<span class="go">c   15     14   15</span>
<span class="go">d    3     13   14</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-327"><a class="reference internal" href="generated/pandas.Series.map.html#pandas.Series.map" title="pandas.Series.map"><code class="xref py py-meth docutils literal"><span class="pre">Series.map()</span></code></a>有一个附加功能，它可以用于轻松地“链接”或“映射”由辅助 Series 定义的值。</span><span class="yiyi-st" id="yiyi-328">这与<a class="reference internal" href="merging.html#merging"><span class="std std-ref">合并/连接功能</span></a>密切相关：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [154]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;six&apos;</span><span class="p">,</span> <span class="s1">&apos;seven&apos;</span><span class="p">,</span> <span class="s1">&apos;six&apos;</span><span class="p">,</span> <span class="s1">&apos;seven&apos;</span><span class="p">,</span> <span class="s1">&apos;six&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>               <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [155]: </span><span class="n">t</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">({</span><span class="s1">&apos;six&apos;</span> <span class="p">:</span> <span class="mf">6.</span><span class="p">,</span> <span class="s1">&apos;seven&apos;</span> <span class="p">:</span> <span class="mf">7.</span><span class="p">})</span>

<span class="gp">In [156]: </span><span class="n">s</span>
<span class="gr">Out[156]: </span>
<span class="go">a      six</span>
<span class="go">b    seven</span>
<span class="go">c      six</span>
<span class="go">d    seven</span>
<span class="go">e      six</span>
<span class="go">dtype: object</span>

<span class="gp">In [157]: </span><span class="n">s</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="gr">Out[157]: </span>
<span class="go">a    6.0</span>
<span class="go">b    7.0</span>
<span class="go">c    6.0</span>
<span class="go">d    7.0</span>
<span class="go">e    6.0</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="applying-with-a-panel">
<span id="basics-apply-panel"></span><h3><span class="yiyi-st" id="yiyi-329">应用于 Panel</span></h3>
<p><span class="yiyi-st" id="yiyi-330">应用于<code class="docutils literal"><span class="pre">Panel</span></code>会将<code class="docutils literal"><span class="pre">Series</span></code>传递给应用的函数。</span><span class="yiyi-st" id="yiyi-331">如果应用函数返回<code class="docutils literal"><span class="pre">Series</span></code>，应用程序的结果将是<code class="docutils literal"><span class="pre">Panel</span></code>。</span><span class="yiyi-st" id="yiyi-332">如果应用函数归约为标量，应用的结果将是<code class="docutils literal"><span class="pre">DataFrame</span></code>。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-333">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-334">0.13.1 之前， <code class="docutils literal"><span class="pre">Panel</span></code> 上的 <code class="docutils literal"><span class="pre">apply</span></code> 仅仅能够处理 <code class="docutils literal"><span class="pre">ufuncs</span></code> (例如 <code class="docutils literal"><span class="pre">np.sum/np.max</span></code>)。</span></p>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [158]: </span><span class="kn">import</span> <span class="nn">pandas.util.testing</span> <span class="kn">as</span> <span class="nn">tm</span>

<span class="gp">In [159]: </span><span class="n">panel</span> <span class="o">=</span> <span class="n">tm</span><span class="o">.</span><span class="n">makePanel</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>

<span class="gp">In [160]: </span><span class="n">panel</span>
<span class="gr">Out[160]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 3 (items) x 5 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: ItemA to ItemC</span>
<span class="go">Major_axis axis: 2000-01-03 00:00:00 to 2000-01-07 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>

<span class="gp">In [161]: </span><span class="n">panel</span><span class="p">[</span><span class="s1">&apos;ItemA&apos;</span><span class="p">]</span>
<span class="gr">Out[161]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-03  0.330418  1.893177  0.801111  0.528154</span>
<span class="go">2000-01-04  1.761200  0.170247  0.445614 -0.029371</span>
<span class="go">2000-01-05  0.567133 -0.916844  1.453046 -0.631117</span>
<span class="go">2000-01-06 -0.251020  0.835024  2.430373 -0.172441</span>
<span class="go">2000-01-07  1.020099  1.259919  0.653093 -1.020485</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-335">转换性的应用</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [162]: </span><span class="n">result</span> <span class="o">=</span> <span class="n">panel</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">*</span><span class="mi">2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">&apos;items&apos;</span><span class="p">)</span>

<span class="gp">In [163]: </span><span class="n">result</span>
<span class="gr">Out[163]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 3 (items) x 5 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: ItemA to ItemC</span>
<span class="go">Major_axis axis: 2000-01-03 00:00:00 to 2000-01-07 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>

<span class="gp">In [164]: </span><span class="n">result</span><span class="p">[</span><span class="s1">&apos;ItemA&apos;</span><span class="p">]</span>
<span class="gr">Out[164]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-03  0.660836  3.786354  1.602222  1.056308</span>
<span class="go">2000-01-04  3.522400  0.340494  0.891228 -0.058742</span>
<span class="go">2000-01-05  1.134266 -1.833689  2.906092 -1.262234</span>
<span class="go">2000-01-06 -0.502039  1.670047  4.860747 -0.344882</span>
<span class="go">2000-01-07  2.040199  2.519838  1.306185 -2.040969</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-336">归约操作</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [165]: </span><span class="n">panel</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">&apos;items&apos;</span><span class="p">)</span>
<span class="gr">Out[165]: </span>
<span class="go">                  A        B        C        D</span>
<span class="go">2000-01-03  float64  float64  float64  float64</span>
<span class="go">2000-01-04  float64  float64  float64  float64</span>
<span class="go">2000-01-05  float64  float64  float64  float64</span>
<span class="go">2000-01-06  float64  float64  float64  float64</span>
<span class="go">2000-01-07  float64  float64  float64  float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-337">类似的归约类型操作</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [166]: </span><span class="n">panel</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">sum</span><span class="p">(),</span> <span class="n">axis</span><span class="o">=</span><span class="s1">&apos;major_axis&apos;</span><span class="p">)</span>
<span class="gr">Out[166]: </span>
<span class="go">      ItemA     ItemB     ItemC</span>
<span class="go">A  3.427831 -2.581431  0.840809</span>
<span class="go">B  3.241522 -1.409935 -1.114512</span>
<span class="go">C  5.783237  0.319672 -0.431906</span>
<span class="go">D -1.325260 -2.914834  0.857043</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-338">最后的归约等同于</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [167]: </span><span class="n">panel</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="s1">&apos;major_axis&apos;</span><span class="p">)</span>
<span class="gr">Out[167]: </span>
<span class="go">      ItemA     ItemB     ItemC</span>
<span class="go">A  3.427831 -2.581431  0.840809</span>
<span class="go">B  3.241522 -1.409935 -1.114512</span>
<span class="go">C  5.783237  0.319672 -0.431906</span>
<span class="go">D -1.325260 -2.914834  0.857043</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-339">转换操作返回<code class="docutils literal"><span class="pre">Panel</span></code>，但沿着<code class="docutils literal"><span class="pre">major_axis</span></code>计算z-score。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [168]: </span><span class="n">result</span> <span class="o">=</span> <span class="n">panel</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span>
<span class="gp">   .....:</span>            <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="o">-</span><span class="n">x</span><span class="o">.</span><span class="n">mean</span><span class="p">())</span><span class="o">/</span><span class="n">x</span><span class="o">.</span><span class="n">std</span><span class="p">(),</span>
<span class="gp">   .....:</span>            <span class="n">axis</span><span class="o">=</span><span class="s1">&apos;major_axis&apos;</span><span class="p">)</span>
<span class="gp">   .....:</span> 

<span class="gp">In [169]: </span><span class="n">result</span>
<span class="gr">Out[169]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 3 (items) x 5 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: ItemA to ItemC</span>
<span class="go">Major_axis axis: 2000-01-03 00:00:00 to 2000-01-07 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>

<span class="gp">In [170]: </span><span class="n">result</span><span class="p">[</span><span class="s1">&apos;ItemA&apos;</span><span class="p">]</span>
<span class="gr">Out[170]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-03 -0.469761  1.156225 -0.441347  1.341731</span>
<span class="go">2000-01-04  1.422763 -0.444015 -0.882647  0.398661</span>
<span class="go">2000-01-05 -0.156654 -1.453694  0.367936 -0.619210</span>
<span class="go">2000-01-06 -1.238841  0.173423  1.581149  0.156654</span>
<span class="go">2000-01-07  0.442494  0.568061 -0.625091 -1.277837</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-340">apply 也可以在<code class="docutils literal"><span class="pre">axis</span></code>参数中接受多个轴。</span><span class="yiyi-st" id="yiyi-341">这会将截面的<code class="docutils literal"><span class="pre">DataFrame</span></code>传递给应用函数。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [171]: </span><span class="n">f</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">((</span><span class="n">x</span><span class="o">.</span><span class="n">T</span><span class="o">-</span><span class="n">x</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span><span class="o">/</span><span class="n">x</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">T</span>

<span class="gp">In [172]: </span><span class="n">result</span> <span class="o">=</span> <span class="n">panel</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">axis</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;items&apos;</span><span class="p">,</span><span class="s1">&apos;major_axis&apos;</span><span class="p">])</span>

<span class="gp">In [173]: </span><span class="n">result</span>
<span class="gr">Out[173]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 4 (items) x 5 (major_axis) x 3 (minor_axis)</span>
<span class="go">Items axis: A to D</span>
<span class="go">Major_axis axis: 2000-01-03 00:00:00 to 2000-01-07 00:00:00</span>
<span class="go">Minor_axis axis: ItemA to ItemC</span>

<span class="gp">In [174]: </span><span class="n">result</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,:,</span><span class="s1">&apos;ItemA&apos;</span><span class="p">]</span>
<span class="gr">Out[174]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-03  0.864236  1.132969  0.557316  0.575106</span>
<span class="go">2000-01-04  0.795745  0.652527  0.534808 -0.070674</span>
<span class="go">2000-01-05 -0.310864  0.558627  1.086688 -1.051477</span>
<span class="go">2000-01-06 -0.001065  0.832460  0.846006  0.043602</span>
<span class="go">2000-01-07  1.128946  1.152469 -0.218186 -0.891680</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-342">这相当于下面：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [175]: </span><span class="n">result</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Panel</span><span class="p">(</span><span class="nb">dict</span><span class="p">([</span> <span class="p">(</span><span class="n">ax</span><span class="p">,</span> <span class="n">f</span><span class="p">(</span><span class="n">panel</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,:,</span><span class="n">ax</span><span class="p">]))</span>
<span class="gp">   .....:</span>                         <span class="k">for</span> <span class="n">ax</span> <span class="ow">in</span> <span class="n">panel</span><span class="o">.</span><span class="n">minor_axis</span> <span class="p">]))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [176]: </span><span class="n">result</span>
<span class="gr">Out[176]: </span>
<span class="go">&lt;class &apos;pandas.core.panel.Panel&apos;&gt;</span>
<span class="go">Dimensions: 4 (items) x 5 (major_axis) x 3 (minor_axis)</span>
<span class="go">Items axis: A to D</span>
<span class="go">Major_axis axis: 2000-01-03 00:00:00 to 2000-01-07 00:00:00</span>
<span class="go">Minor_axis axis: ItemA to ItemC</span>

<span class="gp">In [177]: </span><span class="n">result</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,:,</span><span class="s1">&apos;ItemA&apos;</span><span class="p">]</span>
<span class="gr">Out[177]: </span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-03  0.864236  1.132969  0.557316  0.575106</span>
<span class="go">2000-01-04  0.795745  0.652527  0.534808 -0.070674</span>
<span class="go">2000-01-05 -0.310864  0.558627  1.086688 -1.051477</span>
<span class="go">2000-01-06 -0.001065  0.832460  0.846006  0.043602</span>
<span class="go">2000-01-07  1.128946  1.152469 -0.218186 -0.891680</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="reindexing-and-altering-labels">
<span id="basics-reindexing"></span><h2><span class="yiyi-st" id="yiyi-343">重索引和改变标签</span></h2>
<p><span class="yiyi-st" id="yiyi-344"><a class="reference internal" href="generated/pandas.Series.reindex.html#pandas.Series.reindex" title="pandas.Series.reindex"><code class="xref py py-meth docutils literal"><span class="pre">reindex()</span></code></a>是pandas中的基本数据对齐方法。</span><span class="yiyi-st" id="yiyi-345">它用于实现几乎所有依赖于标签对齐功能的其他功能。</span><span class="yiyi-st" id="yiyi-346"><em>reindex</em>通过指定的索引来匹配值</span><span class="yiyi-st" id="yiyi-347">有以下几个功能：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-348">重新排序现有数据以匹配一组新标签</span></li>
<li><span class="yiyi-st" id="yiyi-349">在没有该标签的数据的标签位置插入缺少值（NA）标记</span></li>
<li><span class="yiyi-st" id="yiyi-350">如果指定，使用逻辑（与使用时间序列数据非常相关），缺少标签的<strong>填充</strong></span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-351">这里有一个简单的例子：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [178]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">])</span>

<span class="gp">In [179]: </span><span class="n">s</span>
<span class="gr">Out[179]: </span>
<span class="go">a   -1.010924</span>
<span class="go">b   -0.672504</span>
<span class="go">c   -1.139222</span>
<span class="go">d    0.354653</span>
<span class="go">e    0.563622</span>
<span class="go">dtype: float64</span>

<span class="gp">In [180]: </span><span class="n">s</span><span class="o">.</span><span class="n">reindex</span><span class="p">([</span><span class="s1">&apos;e&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;f&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">])</span>
<span class="gr">Out[180]: </span>
<span class="go">e    0.563622</span>
<span class="go">b   -0.672504</span>
<span class="go">f         NaN</span>
<span class="go">d    0.354653</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-352">这里，<code class="docutils literal"><span class="pre">f</span></code>标签不包含在系列中，因此在结果中显示为<code class="docutils literal"><span class="pre">NaN</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-353">使用DataFrame，您可以同时重新索引索引和列：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [181]: </span><span class="n">df</span>
<span class="gr">Out[181]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544       NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>
<span class="go">d       NaN  1.124472 -1.101558</span>

<span class="gp">In [182]: </span><span class="n">df</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;f&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">],</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;three&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;one&apos;</span><span class="p">])</span>
<span class="gr">Out[182]: </span>
<span class="go">      three       two       one</span>
<span class="go">c  0.462215 -0.448789  0.011617</span>
<span class="go">f       NaN       NaN       NaN</span>
<span class="go">b -0.177289  1.136249 -0.138894</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-354">为方便起见，您可以使用<a class="reference internal" href="generated/pandas.Series.reindex_axis.html#pandas.Series.reindex_axis" title="pandas.Series.reindex_axis"><code class="xref py py-meth docutils literal"><span class="pre">reindex_axis()</span></code></a>方法，该方法使用标签和关键字<code class="docutils literal"><span class="pre">axis</span></code>参数。</span></p>
<p><span class="yiyi-st" id="yiyi-355">请注意，包含实际轴标签的<code class="docutils literal"><span class="pre">Index</span></code>对象可以在对象之间<strong>共享</strong>。</span><span class="yiyi-st" id="yiyi-356">因此，如果我们有一个Series和一个DataFrame，可以做到以下几点：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [183]: </span><span class="n">rs</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>

<span class="gp">In [184]: </span><span class="n">rs</span>
<span class="gr">Out[184]: </span>
<span class="go">a   -1.010924</span>
<span class="go">b   -0.672504</span>
<span class="go">c   -1.139222</span>
<span class="go">d    0.354653</span>
<span class="go">dtype: float64</span>

<span class="gp">In [185]: </span><span class="n">rs</span><span class="o">.</span><span class="n">index</span> <span class="ow">is</span> <span class="n">df</span><span class="o">.</span><span class="n">index</span>
<span class="gr">Out[185]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-357">这意味着重建索引系列的索引与DataFrame的索引是相同的Python对象。</span></p>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-358">也可以看看</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-359"><a class="reference internal" href="advanced.html#advanced"><span class="std std-ref">MultiIndex / Advanced Indexing</span></a>是一种更简洁的执行重建索引的方法。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-360">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-361">当编写性能敏感的代码时，有一个很好的理由花一些时间成为重建索引忍者：<strong>许多操作在预先对齐的数据</strong>上更快。</span><span class="yiyi-st" id="yiyi-362">在内部添加两个未对齐的DataFrames会触发重建索引步骤。</span><span class="yiyi-st" id="yiyi-363">对于探索性分析，您几乎不会注意到差异（因为<code class="docutils literal"><span class="pre">reindex</span></code>已被大量优化），但是当CPU周期在这里显示一些显式的<code class="docutils literal"><span class="pre">reindex</span></code>调用时， 。</span></p>
</div>
<div class="section" id="reindexing-to-align-with-another-object">
<span id="basics-reindex-like"></span><h3><span class="yiyi-st" id="yiyi-364">Reindexing to align with another object</span></h3>
<p><span class="yiyi-st" id="yiyi-365">您可能希望获取一个对象，并将其轴重命名为与另一个对象相同的标签。</span><span class="yiyi-st" id="yiyi-366">虽然这种语法很简单，尽管很详细，但它是一个常见的操作，<a class="reference internal" href="generated/pandas.DataFrame.reindex_like.html#pandas.DataFrame.reindex_like" title="pandas.DataFrame.reindex_like"><code class="xref py py-meth docutils literal"><span class="pre">reindex_like()</span></code></a>方法可以使这更简单：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [186]: </span><span class="n">df2</span>
<span class="gr">Out[186]: </span>
<span class="go">        one       two</span>
<span class="go">a -0.626544 -0.351587</span>
<span class="go">b -0.138894  1.136249</span>
<span class="go">c  0.011617 -0.448789</span>

<span class="gp">In [187]: </span><span class="n">df3</span>
<span class="gr">Out[187]: </span>
<span class="go">        one       two</span>
<span class="go">a -0.375270 -0.463545</span>
<span class="go">b  0.112379  1.024292</span>
<span class="go">c  0.262891 -0.560746</span>

<span class="gp">In [188]: </span><span class="n">df</span><span class="o">.</span><span class="n">reindex_like</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="gr">Out[188]: </span>
<span class="go">        one       two</span>
<span class="go">a -0.626544 -0.351587</span>
<span class="go">b -0.138894  1.136249</span>
<span class="go">c  0.011617 -0.448789</span>
</pre></div>
</div>
</div>
<div class="section" id="aligning-objects-with-each-other-with-align">
<span id="basics-align"></span><h3><span class="yiyi-st" id="yiyi-367">Aligning objects with each other with <code class="docutils literal"><span class="pre">align</span></code></span></h3>
<p><span class="yiyi-st" id="yiyi-368"><a class="reference internal" href="generated/pandas.Series.align.html#pandas.Series.align" title="pandas.Series.align"><code class="xref py py-meth docutils literal"><span class="pre">align()</span></code></a>方法是同时对齐两个对象的最快方法。</span><span class="yiyi-st" id="yiyi-369">它支持<code class="docutils literal"><span class="pre">join</span></code>参数（与<a class="reference internal" href="merging.html#merging"><span class="std std-ref">joining and merging</span></a>相关）：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-370"><code class="docutils literal"><span class="pre">join=&apos;outer&apos;</span></code>：获取索引的并集（默认）</span></li>
<li><span class="yiyi-st" id="yiyi-371"><code class="docutils literal"><span class="pre">join=&apos;left&apos;</span></code>：使用调用对象的索引</span></li>
<li><span class="yiyi-st" id="yiyi-372"><code class="docutils literal"><span class="pre">join=&apos;right&apos;</span></code>：使用传递的对象的索引</span></li>
<li><span class="yiyi-st" id="yiyi-373"><code class="docutils literal"><span class="pre">join=&apos;inner&apos;</span></code>：与索引相交</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-374">它返回一个带有两个重建索引系列的元组：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [189]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;e&apos;</span><span class="p">])</span>

<span class="gp">In [190]: </span><span class="n">s1</span> <span class="o">=</span> <span class="n">s</span><span class="p">[:</span><span class="mi">4</span><span class="p">]</span>

<span class="gp">In [191]: </span><span class="n">s2</span> <span class="o">=</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>

<span class="gp">In [192]: </span><span class="n">s1</span><span class="o">.</span><span class="n">align</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span>
<span class="gr">Out[192]: </span>
<span class="go">(a   -0.365106</span>
<span class="go"> b    1.092702</span>
<span class="go"> c   -1.481449</span>
<span class="go"> d    1.781190</span>
<span class="go"> e         NaN</span>
<span class="go"> dtype: float64, a         NaN</span>
<span class="go"> b    1.092702</span>
<span class="go"> c   -1.481449</span>
<span class="go"> d    1.781190</span>
<span class="go"> e   -0.031543</span>
<span class="go"> dtype: float64)</span>

<span class="gp">In [193]: </span><span class="n">s1</span><span class="o">.</span><span class="n">align</span><span class="p">(</span><span class="n">s2</span><span class="p">,</span> <span class="n">join</span><span class="o">=</span><span class="s1">&apos;inner&apos;</span><span class="p">)</span>
<span class="gr">Out[193]: </span>
<span class="go">(b    1.092702</span>
<span class="go"> c   -1.481449</span>
<span class="go"> d    1.781190</span>
<span class="go"> dtype: float64, b    1.092702</span>
<span class="go"> c   -1.481449</span>
<span class="go"> d    1.781190</span>
<span class="go"> dtype: float64)</span>

<span class="gp">In [194]: </span><span class="n">s1</span><span class="o">.</span><span class="n">align</span><span class="p">(</span><span class="n">s2</span><span class="p">,</span> <span class="n">join</span><span class="o">=</span><span class="s1">&apos;left&apos;</span><span class="p">)</span>
<span class="gr">Out[194]: </span>
<span class="go">(a   -0.365106</span>
<span class="go"> b    1.092702</span>
<span class="go"> c   -1.481449</span>
<span class="go"> d    1.781190</span>
<span class="go"> dtype: float64, a         NaN</span>
<span class="go"> b    1.092702</span>
<span class="go"> c   -1.481449</span>
<span class="go"> d    1.781190</span>
<span class="go"> dtype: float64)</span>
</pre></div>
</div>
<p id="basics-df-join"><span class="yiyi-st" id="yiyi-375">对于DataFrames，默认情况下，连接方法将应用于索引和列：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [195]: </span><span class="n">df</span><span class="o">.</span><span class="n">align</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span> <span class="n">join</span><span class="o">=</span><span class="s1">&apos;inner&apos;</span><span class="p">)</span>
<span class="gr">Out[195]: </span>
<span class="go">(        one       two</span>
<span class="go"> a -0.626544 -0.351587</span>
<span class="go"> b -0.138894  1.136249</span>
<span class="go"> c  0.011617 -0.448789,         one       two</span>
<span class="go"> a -0.626544 -0.351587</span>
<span class="go"> b -0.138894  1.136249</span>
<span class="go"> c  0.011617 -0.448789)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-376">您也可以传递<code class="docutils literal"><span class="pre">axis</span></code>选项，仅在指定轴上对齐：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [196]: </span><span class="n">df</span><span class="o">.</span><span class="n">align</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span> <span class="n">join</span><span class="o">=</span><span class="s1">&apos;inner&apos;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[196]: </span>
<span class="go">(        one     three       two</span>
<span class="go"> a -0.626544       NaN -0.351587</span>
<span class="go"> b -0.138894 -0.177289  1.136249</span>
<span class="go"> c  0.011617  0.462215 -0.448789,         one       two</span>
<span class="go"> a -0.626544 -0.351587</span>
<span class="go"> b -0.138894  1.136249</span>
<span class="go"> c  0.011617 -0.448789)</span>
</pre></div>
</div>
<p id="basics-align-frame-series"><span class="yiyi-st" id="yiyi-377">如果将Series传递到<a class="reference internal" href="generated/pandas.DataFrame.align.html#pandas.DataFrame.align" title="pandas.DataFrame.align"><code class="xref py py-meth docutils literal"><span class="pre">DataFrame.align()</span></code></a>，则可以选择使用<code class="docutils literal"><span class="pre">axis</span></code>参数在DataFrame的索引或列上对齐两个对象：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [197]: </span><span class="n">df</span><span class="o">.</span><span class="n">align</span><span class="p">(</span><span class="n">df2</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[197]: </span>
<span class="go">(        one     three       two</span>
<span class="go"> a -0.626544       NaN -0.351587</span>
<span class="go"> b -0.138894 -0.177289  1.136249</span>
<span class="go"> c  0.011617  0.462215 -0.448789</span>
<span class="go"> d       NaN  1.124472 -1.101558, one     -0.626544</span>
<span class="go"> three         NaN</span>
<span class="go"> two     -0.351587</span>
<span class="go"> Name: a, dtype: float64)</span>
</pre></div>
</div>
</div>
<div class="section" id="filling-while-reindexing">
<span id="basics-reindex-fill"></span><h3><span class="yiyi-st" id="yiyi-378">Filling while reindexing</span></h3>
<p><span class="yiyi-st" id="yiyi-379"><a class="reference internal" href="generated/pandas.Series.reindex.html#pandas.Series.reindex" title="pandas.Series.reindex"><code class="xref py py-meth docutils literal"><span class="pre">reindex()</span></code></a>采用可选参数<code class="docutils literal"><span class="pre">method</span></code>，它是从下表中选择的填充方法：</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="38%">
<col width="63%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-380">方法</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-381">行动</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-382">pad / ffill</span></td>
<td><span class="yiyi-st" id="yiyi-383">向前填充值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-384">bfill / backfill</span></td>
<td><span class="yiyi-st" id="yiyi-385">向后填充值</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-386">最近</span></td>
<td><span class="yiyi-st" id="yiyi-387">从最近的索引值填充</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-388">我们在一个简单的系列上说明这些填充方法：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [198]: </span><span class="n">rng</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;1/3/2000&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>

<span class="gp">In [199]: </span><span class="n">ts</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">rng</span><span class="p">)</span>

<span class="gp">In [200]: </span><span class="n">ts2</span> <span class="o">=</span> <span class="n">ts</span><span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]</span>

<span class="gp">In [201]: </span><span class="n">ts</span>
<span class="gr">Out[201]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-04    0.604244</span>
<span class="go">2000-01-05   -0.487265</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-07    0.327007</span>
<span class="go">2000-01-08    1.053639</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">2000-01-10    0.082065</span>
<span class="go">Freq: D, dtype: float64</span>

<span class="gp">In [202]: </span><span class="n">ts2</span>
<span class="gr">Out[202]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">dtype: float64</span>

<span class="gp">In [203]: </span><span class="n">ts2</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>
<span class="gr">Out[203]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-04         NaN</span>
<span class="go">2000-01-05         NaN</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-07         NaN</span>
<span class="go">2000-01-08         NaN</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">2000-01-10         NaN</span>
<span class="go">Freq: D, dtype: float64</span>

<span class="gp">In [204]: </span><span class="n">ts2</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&apos;ffill&apos;</span><span class="p">)</span>
<span class="gr">Out[204]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-04    0.480993</span>
<span class="go">2000-01-05    0.480993</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-07    1.990533</span>
<span class="go">2000-01-08    1.990533</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">2000-01-10   -2.927808</span>
<span class="go">Freq: D, dtype: float64</span>

<span class="gp">In [205]: </span><span class="n">ts2</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&apos;bfill&apos;</span><span class="p">)</span>
<span class="gr">Out[205]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-04    1.990533</span>
<span class="go">2000-01-05    1.990533</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-07   -2.927808</span>
<span class="go">2000-01-08   -2.927808</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">2000-01-10         NaN</span>
<span class="go">Freq: D, dtype: float64</span>

<span class="gp">In [206]: </span><span class="n">ts2</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&apos;nearest&apos;</span><span class="p">)</span>
<span class="gr">Out[206]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-04    0.480993</span>
<span class="go">2000-01-05    1.990533</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-07    1.990533</span>
<span class="go">2000-01-08   -2.927808</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">2000-01-10   -2.927808</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-389">这些方法要求索引<strong>有序</strong>增加或减少。</span></p>
<p><span class="yiyi-st" id="yiyi-390">请注意，使用<a class="reference internal" href="missing_data.html#missing-data-fillna"><span class="std std-ref">fillna</span></a>（除了<code class="docutils literal"><span class="pre">method=&apos;nearest&apos;</span></code>）或<a class="reference internal" href="missing_data.html#missing-data-interpolate"><span class="std std-ref">interpolate</span></a>可以实现相同的结果：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [207]: </span><span class="n">ts2</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">)</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">method</span><span class="o">=</span><span class="s1">&apos;ffill&apos;</span><span class="p">)</span>
<span class="gr">Out[207]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-04    0.480993</span>
<span class="go">2000-01-05    0.480993</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-07    1.990533</span>
<span class="go">2000-01-08    1.990533</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">2000-01-10   -2.927808</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-391">如果索引不是单调递增或递减，<a class="reference internal" href="generated/pandas.Series.reindex.html#pandas.Series.reindex" title="pandas.Series.reindex"><code class="xref py py-meth docutils literal"><span class="pre">reindex()</span></code></a>将引发ValueError。</span><span class="yiyi-st" id="yiyi-392"><a class="reference internal" href="generated/pandas.Series.fillna.html#pandas.Series.fillna" title="pandas.Series.fillna"><code class="xref py py-meth docutils literal"><span class="pre">fillna()</span></code></a>和<a class="reference internal" href="generated/pandas.Series.interpolate.html#pandas.Series.interpolate" title="pandas.Series.interpolate"><code class="xref py py-meth docutils literal"><span class="pre">interpolate()</span></code></a>不会对索引的顺序进行任何检查。</span></p>
</div>
<div class="section" id="limits-on-filling-while-reindexing">
<span id="basics-limits-on-reindex-fill"></span><h3><span class="yiyi-st" id="yiyi-393">Limits on filling while reindexing</span></h3>
<p><span class="yiyi-st" id="yiyi-394"><code class="docutils literal"><span class="pre">limit</span></code>和<code class="docutils literal"><span class="pre">tolerance</span></code>参数在重建索引时提供对填充的额外控制。</span><span class="yiyi-st" id="yiyi-395">Limit指定连续匹配的最大计数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [208]: </span><span class="n">ts2</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&apos;ffill&apos;</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[208]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-04    0.480993</span>
<span class="go">2000-01-05         NaN</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-07    1.990533</span>
<span class="go">2000-01-08         NaN</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">2000-01-10   -2.927808</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-396">相反，公差指定索引和索引器值之间的最大距离：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [209]: </span><span class="n">ts2</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">index</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&apos;ffill&apos;</span><span class="p">,</span> <span class="n">tolerance</span><span class="o">=</span><span class="s1">&apos;1 day&apos;</span><span class="p">)</span>
<span class="gr">Out[209]: </span>
<span class="go">2000-01-03    0.480993</span>
<span class="go">2000-01-04    0.480993</span>
<span class="go">2000-01-05         NaN</span>
<span class="go">2000-01-06    1.990533</span>
<span class="go">2000-01-07    1.990533</span>
<span class="go">2000-01-08         NaN</span>
<span class="go">2000-01-09   -2.927808</span>
<span class="go">2000-01-10   -2.927808</span>
<span class="go">Freq: D, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-397">注意当在<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>，<code class="docutils literal"><span class="pre">TimedeltaIndex</span></code>或<code class="docutils literal"><span class="pre">PeriodIndex</span></code>上使用时，<code class="docutils literal"><span class="pre">tolerance</span></code>将强制为<code class="docutils literal"><span class="pre">Timedelta</span></code></span><span class="yiyi-st" id="yiyi-398">这允许您使用适当的字符串指定公差。</span></p>
</div>
<div class="section" id="dropping-labels-from-an-axis">
<span id="basics-drop"></span><h3><span class="yiyi-st" id="yiyi-399">Dropping labels from an axis</span></h3>
<p><span class="yiyi-st" id="yiyi-400">与<code class="docutils literal"><span class="pre">reindex</span></code>密切相关的方法是<a class="reference internal" href="generated/pandas.DataFrame.drop.html#pandas.DataFrame.drop" title="pandas.DataFrame.drop"><code class="xref py py-meth docutils literal"><span class="pre">drop()</span></code></a>函数。</span><span class="yiyi-st" id="yiyi-401">它从轴删除一组标签：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [210]: </span><span class="n">df</span>
<span class="gr">Out[210]: </span>
<span class="go">        one     three       two</span>
<span class="go">a -0.626544       NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>
<span class="go">d       NaN  1.124472 -1.101558</span>

<span class="gp">In [211]: </span><span class="n">df</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[211]: </span>
<span class="go">        one     three       two</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>

<span class="gp">In [212]: </span><span class="n">df</span><span class="o">.</span><span class="n">drop</span><span class="p">([</span><span class="s1">&apos;one&apos;</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[212]: </span>
<span class="go">      three       two</span>
<span class="go">a       NaN -0.351587</span>
<span class="go">b -0.177289  1.136249</span>
<span class="go">c  0.462215 -0.448789</span>
<span class="go">d  1.124472 -1.101558</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-402">注意下面的也可以，但是不太明显/干净：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [213]: </span><span class="n">df</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">difference</span><span class="p">([</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">]))</span>
<span class="gr">Out[213]: </span>
<span class="go">        one     three       two</span>
<span class="go">b -0.138894 -0.177289  1.136249</span>
<span class="go">c  0.011617  0.462215 -0.448789</span>
</pre></div>
</div>
</div>
<div class="section" id="renaming-mapping-labels">
<span id="basics-rename"></span><h3><span class="yiyi-st" id="yiyi-403">Renaming / mapping labels</span></h3>
<p><span class="yiyi-st" id="yiyi-404"><a class="reference internal" href="generated/pandas.DataFrame.rename.html#pandas.DataFrame.rename" title="pandas.DataFrame.rename"><code class="xref py py-meth docutils literal"><span class="pre">rename()</span></code></a>方法允许您根据某些映射（字典或系列）或任意函数重新标记轴。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [214]: </span><span class="n">s</span>
<span class="gr">Out[214]: </span>
<span class="go">a   -0.365106</span>
<span class="go">b    1.092702</span>
<span class="go">c   -1.481449</span>
<span class="go">d    1.781190</span>
<span class="go">e   -0.031543</span>
<span class="go">dtype: float64</span>

<span class="gp">In [215]: </span><span class="n">s</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="nb">str</span><span class="o">.</span><span class="n">upper</span><span class="p">)</span>
<span class="gr">Out[215]: </span>
<span class="go">A   -0.365106</span>
<span class="go">B    1.092702</span>
<span class="go">C   -1.481449</span>
<span class="go">D    1.781190</span>
<span class="go">E   -0.031543</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-405">如果传递一个函数，它必须在使用任何标签调用时返回一个值（并且必须生成一组唯一值）。</span><span class="yiyi-st" id="yiyi-406">也可以使用dict或系列：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [216]: </span><span class="n">df</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;one&apos;</span> <span class="p">:</span> <span class="s1">&apos;foo&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span> <span class="p">:</span> <span class="s1">&apos;bar&apos;</span><span class="p">},</span>
<span class="gp">   .....:</span>           <span class="n">index</span><span class="o">=</span><span class="p">{</span><span class="s1">&apos;a&apos;</span> <span class="p">:</span> <span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span> <span class="p">:</span> <span class="s1">&apos;banana&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span> <span class="p">:</span> <span class="s1">&apos;durian&apos;</span><span class="p">})</span>
<span class="gp">   .....:</span> 
<span class="gr">Out[216]: </span>
<span class="go">             foo     three       bar</span>
<span class="go">apple  -0.626544       NaN -0.351587</span>
<span class="go">banana -0.138894 -0.177289  1.136249</span>
<span class="go">c       0.011617  0.462215 -0.448789</span>
<span class="go">durian       NaN  1.124472 -1.101558</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-407">如果映射不包括列/索引标签，则不会重命名。</span><span class="yiyi-st" id="yiyi-408">此外，映射中的额外标签不会抛出错误。</span></p>
<p><span class="yiyi-st" id="yiyi-409"><a class="reference internal" href="generated/pandas.DataFrame.rename.html#pandas.DataFrame.rename" title="pandas.DataFrame.rename"><code class="xref py py-meth docutils literal"><span class="pre">rename()</span></code></a>方法还提供了一个<code class="docutils literal"><span class="pre">inplace</span></code>命名参数，默认为<code class="docutils literal"><span class="pre">False</span></code>并复制基础数据。</span><span class="yiyi-st" id="yiyi-410">传递<code class="docutils literal"><span class="pre">inplace=True</span></code>可重新命名数据。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-411"><span class="versionmodified">版本0.18.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-412">最后，<a class="reference internal" href="generated/pandas.Series.rename.html#pandas.Series.rename" title="pandas.Series.rename"><code class="xref py py-meth docutils literal"><span class="pre">rename()</span></code></a>也接受一个标量或列表，用于更改<code class="docutils literal"><span class="pre">Series.name</span></code>属性。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [217]: </span><span class="n">s</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="s2">&quot;scalar-name&quot;</span><span class="p">)</span>
<span class="gr">Out[217]: </span>
<span class="go">a   -0.365106</span>
<span class="go">b    1.092702</span>
<span class="go">c   -1.481449</span>
<span class="go">d    1.781190</span>
<span class="go">e   -0.031543</span>
<span class="go">Name: scalar-name, dtype: float64</span>
</pre></div>
</div>
<p id="basics-rename-axis"><span class="yiyi-st" id="yiyi-413">Panel类具有相关的<a class="reference internal" href="generated/pandas.Panel.rename_axis.html#pandas.Panel.rename_axis" title="pandas.Panel.rename_axis"><code class="xref py py-meth docutils literal"><span class="pre">rename_axis()</span></code></a>类，可以重命名其三个轴中的任何一个。</span></p>
</div>
</div>
<div class="section" id="iteration">
<span id="basics-iteration"></span><h2><span class="yiyi-st" id="yiyi-414">Iteration</span></h2>
<p><span class="yiyi-st" id="yiyi-415">对pandas对象基本迭代的行为取决于类型。</span><span class="yiyi-st" id="yiyi-416">当对一个Series迭代时，它被认为是数组类，并且基本迭代产生值。</span><span class="yiyi-st" id="yiyi-417">其他数据结构，如DataFrame和Panel，遵循类似于对象的“键”的迭代惯例。</span></p>
<p><span class="yiyi-st" id="yiyi-418">简而言之，基本迭代（<code class="docutils literal"><span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">object</span></code>）会产生：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-419"><strong>Series</strong>：值</span></li>
<li><span class="yiyi-st" id="yiyi-420"><strong>DataFrame</strong>：列标签</span></li>
<li><span class="yiyi-st" id="yiyi-421"><strong>面板</strong>：项目标签</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-422">因此，例如，对DataFrame进行迭代可以得到列名：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [218]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;col1&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="s1">&apos;col2&apos;</span> <span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">)},</span>
<span class="gp">   .....:</span>                   <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="gp">In [219]: </span><span class="k">for</span> <span class="n">col</span> <span class="ow">in</span> <span class="n">df</span><span class="p">:</span>
<span class="gp">   .....:</span>     <span class="k">print</span><span class="p">(</span><span class="n">col</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">col1</span>
<span class="go">col2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-423">Pandas对象也有像dict一样的<a class="reference internal" href="generated/pandas.DataFrame.iteritems.html#pandas.DataFrame.iteritems" title="pandas.DataFrame.iteritems"><code class="xref py py-meth docutils literal"><span class="pre">iteritems()</span></code></a>方法来遍历（key，value）对。</span></p>
<p><span class="yiyi-st" id="yiyi-424">要遍历DataFrame的行，可以使用以下方法：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-425"><a class="reference internal" href="generated/pandas.DataFrame.iterrows.html#pandas.DataFrame.iterrows" title="pandas.DataFrame.iterrows"><code class="xref py py-meth docutils literal"><span class="pre">iterrows()</span></code></a>：将DataFrame的行迭代为（index，Series）对。</span><span class="yiyi-st" id="yiyi-426">这将行转换为Series对象，这可以更改dtypes并具有一些性能影响。</span></li>
<li><span class="yiyi-st" id="yiyi-427"><a class="reference internal" href="generated/pandas.DataFrame.itertuples.html#pandas.DataFrame.itertuples" title="pandas.DataFrame.itertuples"><code class="xref py py-meth docutils literal"><span class="pre">itertuples()</span></code></a>：将DataFrame的行迭代为值的namedtuples。</span><span class="yiyi-st" id="yiyi-428">这比<a class="reference internal" href="generated/pandas.DataFrame.iterrows.html#pandas.DataFrame.iterrows" title="pandas.DataFrame.iterrows"><code class="xref py py-meth docutils literal"><span class="pre">iterrows()</span></code></a>快得多，在大多数情况下，最好使用它来遍历DataFrame的值。</span></li>
</ul>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-429">警告</span></p>
<p><span class="yiyi-st" id="yiyi-430">通过pandas对象进行迭代通常是<strong>慢</strong>。</span><span class="yiyi-st" id="yiyi-431">在许多情况下，不需要对行进行手动迭代，可以使用以下方法之一来避免：</span></p>
<ul class="last simple">
<li><span class="yiyi-st" id="yiyi-432">查找<em>向量化</em>解决方案：许多操作可以使用内置方法或numpy函数执行，（布尔）索引，...</span></li>
<li><span class="yiyi-st" id="yiyi-433">当你有一个功能不能一次应用在整个DataFrame /系列，最好使用<a class="reference internal" href="generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply" title="pandas.DataFrame.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>而不是对所有值进行迭代。</span><span class="yiyi-st" id="yiyi-434">请参阅<a class="reference internal" href="#basics-apply"><span class="std std-ref">function application</span></a>上的文档。</span></li>
<li><span class="yiyi-st" id="yiyi-435">如果你需要对值进行迭代操作，但性能很重要，可以考虑使用例如。 cython或numba。</span><span class="yiyi-st" id="yiyi-436">有关此方法的一些示例，请参见<a class="reference internal" href="enhancingperf.html#enhancingperf"><span class="std std-ref">enhancing performance</span></a>部分。</span></li>
</ul>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-437">警告</span></p>
<p><span class="yiyi-st" id="yiyi-438">您应该<strong>不要修改</strong>您要迭代的内容。</span><span class="yiyi-st" id="yiyi-439">这不能保证在所有情况下都可用。</span><span class="yiyi-st" id="yiyi-440">根据数据类型，迭代器返回一个副本而不是一个视图，并且写入它将没有效果！</span></p>
<p><span class="yiyi-st" id="yiyi-441">例如，在以下情况下，设置值不起作用：</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [220]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">]})</span>

<span class="gp">In [221]: </span><span class="k">for</span> <span class="n">index</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">iterrows</span><span class="p">():</span>
<span class="gp">   .....:</span>     <span class="n">row</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10</span>
<span class="gp">   .....:</span> 

<span class="gp">In [222]: </span><span class="n">df</span>
<span class="gr">Out[222]: </span>
<span class="go">   a  b</span>
<span class="go">0  1  a</span>
<span class="go">1  2  b</span>
<span class="go">2  3  c</span>
</pre></div>
</div>
</div>
<div class="section" id="iteritems">
<h3><span class="yiyi-st" id="yiyi-442">iteritems</span></h3>
<p><span class="yiyi-st" id="yiyi-443">与dict类似接口一致，<a class="reference internal" href="generated/pandas.DataFrame.iteritems.html#pandas.DataFrame.iteritems" title="pandas.DataFrame.iteritems"><code class="xref py py-meth docutils literal"><span class="pre">iteritems()</span></code></a>通过键值对进行迭代：</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-444"><strong>系列</strong>：（索引，标量值）对</span></li>
<li><span class="yiyi-st" id="yiyi-445"><strong>DataFrame</strong>：（列，系列）对</span></li>
<li><span class="yiyi-st" id="yiyi-446"><strong>面板</strong>：（item，DataFrame）对</span></li>
</ul>
<p><span class="yiyi-st" id="yiyi-447">例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [223]: </span><span class="k">for</span> <span class="n">item</span><span class="p">,</span> <span class="n">frame</span> <span class="ow">in</span> <span class="n">wp</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
<span class="gp">   .....:</span>     <span class="k">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
<span class="gp">   .....:</span>     <span class="k">print</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">Item1</span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01 -1.032011  0.969818 -0.962723  1.382083</span>
<span class="go">2000-01-02 -0.938794  0.669142 -0.433567 -0.273610</span>
<span class="go">2000-01-03  0.680433 -0.308450 -0.276099 -1.821168</span>
<span class="go">2000-01-04 -1.993606 -1.927385 -2.027924  1.624972</span>
<span class="go">2000-01-05  0.551135  3.059267  0.455264 -0.030740</span>
<span class="go">Item2</span>
<span class="go">                   A         B         C         D</span>
<span class="go">2000-01-01  0.935716  1.061192 -2.107852  0.199905</span>
<span class="go">2000-01-02  0.323586 -0.641630 -0.587514  0.053897</span>
<span class="go">2000-01-03  0.194889 -0.381994  0.318587  2.089075</span>
<span class="go">2000-01-04 -0.728293 -0.090255 -0.748199  1.318931</span>
<span class="go">2000-01-05 -2.029766  0.792652  0.461007 -0.542749</span>
</pre></div>
</div>
</div>
<div class="section" id="iterrows">
<span id="basics-iterrows"></span><h3><span class="yiyi-st" id="yiyi-448">iterrows</span></h3>
<p><span class="yiyi-st" id="yiyi-449"><a class="reference internal" href="generated/pandas.DataFrame.iterrows.html#pandas.DataFrame.iterrows" title="pandas.DataFrame.iterrows"><code class="xref py py-meth docutils literal"><span class="pre">iterrows()</span></code></a>允许您将DataFrame的行作为Series对象进行迭代。</span><span class="yiyi-st" id="yiyi-450">它返回一个迭代器，产生每个索引值以及包含每行中数据的Series：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [224]: </span><span class="k">for</span> <span class="n">row_index</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">iterrows</span><span class="p">():</span>
<span class="gp">   .....:</span>     <span class="k">print</span><span class="p">(</span><span class="s1">&apos;</span><span class="si">%s</span><span class="se">\n</span><span class="si">%s</span><span class="s1">&apos;</span> <span class="o">%</span> <span class="p">(</span><span class="n">row_index</span><span class="p">,</span> <span class="n">row</span><span class="p">))</span>
<span class="gp">   .....:</span> 
<span class="go">0</span>
<span class="go">a    1</span>
<span class="go">b    a</span>
<span class="go">Name: 0, dtype: object</span>
<span class="go">1</span>
<span class="go">a    2</span>
<span class="go">b    b</span>
<span class="go">Name: 1, dtype: object</span>
<span class="go">2</span>
<span class="go">a    3</span>
<span class="go">b    c</span>
<span class="go">Name: 2, dtype: object</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-451">注意</span></p>
<p><span class="yiyi-st" id="yiyi-452">因为<a class="reference internal" href="generated/pandas.DataFrame.iterrows.html#pandas.DataFrame.iterrows" title="pandas.DataFrame.iterrows"><code class="xref py py-meth docutils literal"><span class="pre">iterrows()</span></code></a>为每一行返回一个Series，所以它<strong>不</strong>保留跨行的Dtypes（对于DataFrames，Dtypes保留在列之间）。</span><span class="yiyi-st" id="yiyi-453">例如，</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [225]: </span><span class="n">df_orig</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">]],</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;int&apos;</span><span class="p">,</span> <span class="s1">&apos;float&apos;</span><span class="p">])</span>

<span class="gp">In [226]: </span><span class="n">df_orig</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[226]: </span>
<span class="go">int        int64</span>
<span class="go">float    float64</span>
<span class="go">dtype: object</span>

<span class="gp">In [227]: </span><span class="n">row</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">df_orig</span><span class="o">.</span><span class="n">iterrows</span><span class="p">())[</span><span class="mi">1</span><span class="p">]</span>

<span class="gp">In [228]: </span><span class="n">row</span>
<span class="gr">Out[228]: </span>
<span class="go">int      1.0</span>
<span class="go">float    1.5</span>
<span class="go">Name: 0, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-454">以系列形式返回的<code class="docutils literal"><span class="pre">row</span></code>中的所有值现在都转换为浮动，也是列<cite>x</cite>中的原始整数值：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [229]: </span><span class="n">row</span><span class="p">[</span><span class="s1">&apos;int&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">dtype</span>
<span class="gr">Out[229]: </span><span class="n">dtype</span><span class="p">(</span><span class="s1">&apos;float64&apos;</span><span class="p">)</span>

<span class="gp">In [230]: </span><span class="n">df_orig</span><span class="p">[</span><span class="s1">&apos;int&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">dtype</span>
<span class="gr">Out[230]: </span><span class="n">dtype</span><span class="p">(</span><span class="s1">&apos;int64&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p class="last"><span class="yiyi-st" id="yiyi-455">要在遍历行时保留dtypes，最好使用<a class="reference internal" href="generated/pandas.DataFrame.itertuples.html#pandas.DataFrame.itertuples" title="pandas.DataFrame.itertuples"><code class="xref py py-meth docutils literal"><span class="pre">itertuples()</span></code></a>，它返回值的namedtuples，通常比<code class="docutils literal"><span class="pre">iterrows</span></code>快得多。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-456">例如，一个设计的方式来转置DataFrame将是：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [231]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;x&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;y&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]})</span>

<span class="gp">In [232]: </span><span class="k">print</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="go">   x  y</span>
<span class="go">0  1  4</span>
<span class="go">1  2  5</span>
<span class="go">2  3  6</span>

<span class="gp">In [233]: </span><span class="k">print</span><span class="p">(</span><span class="n">df2</span><span class="o">.</span><span class="n">T</span><span class="p">)</span>
<span class="go">   0  1  2</span>
<span class="go">x  1  2  3</span>
<span class="go">y  4  5  6</span>

<span class="gp">In [234]: </span><span class="n">df2_t</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">((</span><span class="n">idx</span><span class="p">,</span><span class="n">values</span><span class="p">)</span> <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">values</span> <span class="ow">in</span> <span class="n">df2</span><span class="o">.</span><span class="n">iterrows</span><span class="p">()))</span>

<span class="gp">In [235]: </span><span class="k">print</span><span class="p">(</span><span class="n">df2_t</span><span class="p">)</span>
<span class="go">   0  1  2</span>
<span class="go">x  1  2  3</span>
<span class="go">y  4  5  6</span>
</pre></div>
</div>
</div>
<div class="section" id="itertuples">
<h3><span class="yiyi-st" id="yiyi-457">itertuples</span></h3>
<p><span class="yiyi-st" id="yiyi-458"><a class="reference internal" href="generated/pandas.DataFrame.itertuples.html#pandas.DataFrame.itertuples" title="pandas.DataFrame.itertuples"><code class="xref py py-meth docutils literal"><span class="pre">itertuples()</span></code></a>方法将返回一个迭代器，为DataFrame中的每一行生成一个namedtuple。</span><span class="yiyi-st" id="yiyi-459">元组的第一个元素将是行的对应索引值，而剩余的值是行值。</span></p>
<p><span class="yiyi-st" id="yiyi-460">例如，</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [236]: </span><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">itertuples</span><span class="p">():</span>
<span class="gp">   .....:</span>     <span class="k">print</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
<span class="gp">   .....:</span> 
<span class="go">Pandas(Index=0, a=1, b=&apos;a&apos;)</span>
<span class="go">Pandas(Index=1, a=2, b=&apos;b&apos;)</span>
<span class="go">Pandas(Index=2, a=3, b=&apos;c&apos;)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-461">此方法不会将该行转换为Series对象，而只返回namedtuple中的值。</span><span class="yiyi-st" id="yiyi-462">因此，<a class="reference internal" href="generated/pandas.DataFrame.itertuples.html#pandas.DataFrame.itertuples" title="pandas.DataFrame.itertuples"><code class="xref py py-meth docutils literal"><span class="pre">itertuples()</span></code></a>保留值的数据类型，通常比<a class="reference internal" href="generated/pandas.DataFrame.iterrows.html#pandas.DataFrame.iterrows" title="pandas.DataFrame.iterrows"><code class="xref py py-meth docutils literal"><span class="pre">iterrows()</span></code></a>更快。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-463">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-464">如果列名称是无效的Python标识符，重复，或以下划线开头，则列名称将重命名为位置名称。</span><span class="yiyi-st" id="yiyi-465">在大量列（&gt; 255）的情况下，返回常规元组。</span></p>
</div>
</div>
</div>
<div class="section" id="dt-accessor">
<span id="basics-dt-accessors"></span><h2><span class="yiyi-st" id="yiyi-466">.dt accessor</span></h2>
<p><span class="yiyi-st" id="yiyi-467"><code class="docutils literal"><span class="pre">Series</span></code>具有访问器，以简洁地返回类似于系列的<em>值</em>的datetime属性，如果它是类似于系列的日期时间/期间。</span><span class="yiyi-st" id="yiyi-468">这将返回一个系列，索引像现有的系列。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># datetime</span>
<span class="gp">In [237]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101 09:10:12&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>

<span class="gp">In [238]: </span><span class="n">s</span>
<span class="gr">Out[238]: </span>
<span class="go">0   2013-01-01 09:10:12</span>
<span class="go">1   2013-01-02 09:10:12</span>
<span class="go">2   2013-01-03 09:10:12</span>
<span class="go">3   2013-01-04 09:10:12</span>
<span class="go">dtype: datetime64[ns]</span>

<span class="gp">In [239]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">hour</span>
<span class="gr">Out[239]: </span>
<span class="go">0    9</span>
<span class="go">1    9</span>
<span class="go">2    9</span>
<span class="go">3    9</span>
<span class="go">dtype: int64</span>

<span class="gp">In [240]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">second</span>
<span class="gr">Out[240]: </span>
<span class="go">0    12</span>
<span class="go">1    12</span>
<span class="go">2    12</span>
<span class="go">3    12</span>
<span class="go">dtype: int64</span>

<span class="gp">In [241]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">day</span>
<span class="gr">Out[241]: </span>
<span class="go">0    1</span>
<span class="go">1    2</span>
<span class="go">2    3</span>
<span class="go">3    4</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-469">这使得很好的表达式像这样：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [242]: </span><span class="n">s</span><span class="p">[</span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">day</span><span class="o">==</span><span class="mi">2</span><span class="p">]</span>
<span class="gr">Out[242]: </span>
<span class="go">1   2013-01-02 09:10:12</span>
<span class="go">dtype: datetime64[ns]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-470">你可以使用tz_localize轻松转换时区</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [243]: </span><span class="n">stz</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>

<span class="gp">In [244]: </span><span class="n">stz</span>
<span class="gr">Out[244]: </span>
<span class="go">0   2013-01-01 09:10:12-05:00</span>
<span class="go">1   2013-01-02 09:10:12-05:00</span>
<span class="go">2   2013-01-03 09:10:12-05:00</span>
<span class="go">3   2013-01-04 09:10:12-05:00</span>
<span class="go">dtype: datetime64[ns, US/Eastern]</span>

<span class="gp">In [245]: </span><span class="n">stz</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">tz</span>
<span class="gr">Out[245]: </span><span class="o">&lt;</span><span class="n">DstTzInfo</span> <span class="s1">&apos;US/Eastern&apos;</span> <span class="n">LMT</span><span class="o">-</span><span class="mi">1</span> <span class="n">day</span><span class="p">,</span> <span class="mi">19</span><span class="p">:</span><span class="mo">04</span><span class="p">:</span><span class="mo">00</span> <span class="n">STD</span><span class="o">&gt;</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-471">您还可以链接这些类型的操作：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [246]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">tz_localize</span><span class="p">(</span><span class="s1">&apos;UTC&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">tz_convert</span><span class="p">(</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>
<span class="gr">Out[246]: </span>
<span class="go">0   2013-01-01 04:10:12-05:00</span>
<span class="go">1   2013-01-02 04:10:12-05:00</span>
<span class="go">2   2013-01-03 04:10:12-05:00</span>
<span class="go">3   2013-01-04 04:10:12-05:00</span>
<span class="go">dtype: datetime64[ns, US/Eastern]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-472">您还可以将datetime值格式化为字符串<a class="reference internal" href="generated/pandas.Series.dt.strftime.html#pandas.Series.dt.strftime" title="pandas.Series.dt.strftime"><code class="xref py py-meth docutils literal"><span class="pre">Series.dt.strftime()</span></code></a>，该格式支持与标准<a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime.strftime" title="(in Python v3.6)"><code class="xref py py-meth docutils literal"><span class="pre">strftime()</span></code></a>相同的格式。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># DatetimeIndex</span>
<span class="gp">In [247]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>

<span class="gp">In [248]: </span><span class="n">s</span>
<span class="gr">Out[248]: </span>
<span class="go">0   2013-01-01</span>
<span class="go">1   2013-01-02</span>
<span class="go">2   2013-01-03</span>
<span class="go">3   2013-01-04</span>
<span class="go">dtype: datetime64[ns]</span>

<span class="gp">In [249]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&apos;%Y/%m/</span><span class="si">%d</span><span class="s1">&apos;</span><span class="p">)</span>
<span class="gr">Out[249]: </span>
<span class="go">0    2013/01/01</span>
<span class="go">1    2013/01/02</span>
<span class="go">2    2013/01/03</span>
<span class="go">3    2013/01/04</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># PeriodIndex</span>
<span class="gp">In [250]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>

<span class="gp">In [251]: </span><span class="n">s</span>
<span class="gr">Out[251]: </span>
<span class="go">0   2013-01-01</span>
<span class="go">1   2013-01-02</span>
<span class="go">2   2013-01-03</span>
<span class="go">3   2013-01-04</span>
<span class="go">dtype: object</span>

<span class="gp">In [252]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&apos;%Y/%m/</span><span class="si">%d</span><span class="s1">&apos;</span><span class="p">)</span>
<span class="gr">Out[252]: </span>
<span class="go">0    2013/01/01</span>
<span class="go">1    2013/01/02</span>
<span class="go">2    2013/01/03</span>
<span class="go">3    2013/01/04</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-473"><code class="docutils literal"><span class="pre">.dt</span></code>存取器适用于period和timedelta类型。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># period</span>
<span class="gp">In [253]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">period_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;D&apos;</span><span class="p">))</span>

<span class="gp">In [254]: </span><span class="n">s</span>
<span class="gr">Out[254]: </span>
<span class="go">0   2013-01-01</span>
<span class="go">1   2013-01-02</span>
<span class="go">2   2013-01-03</span>
<span class="go">3   2013-01-04</span>
<span class="go">dtype: object</span>

<span class="gp">In [255]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">year</span>
<span class="gr">Out[255]: </span>
<span class="go">0    2013</span>
<span class="go">1    2013</span>
<span class="go">2    2013</span>
<span class="go">3    2013</span>
<span class="go">dtype: int64</span>

<span class="gp">In [256]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">day</span>
<span class="gr">Out[256]: </span>
<span class="go">0    1</span>
<span class="go">1    2</span>
<span class="go">2    3</span>
<span class="go">3    4</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># timedelta</span>
<span class="gp">In [257]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">timedelta_range</span><span class="p">(</span><span class="s1">&apos;1 day 00:00:05&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="s1">&apos;s&apos;</span><span class="p">))</span>

<span class="gp">In [258]: </span><span class="n">s</span>
<span class="gr">Out[258]: </span>
<span class="go">0   1 days 00:00:05</span>
<span class="go">1   1 days 00:00:06</span>
<span class="go">2   1 days 00:00:07</span>
<span class="go">3   1 days 00:00:08</span>
<span class="go">dtype: timedelta64[ns]</span>

<span class="gp">In [259]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">days</span>
<span class="gr">Out[259]: </span>
<span class="go">0    1</span>
<span class="go">1    1</span>
<span class="go">2    1</span>
<span class="go">3    1</span>
<span class="go">dtype: int64</span>

<span class="gp">In [260]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">seconds</span>
<span class="gr">Out[260]: </span>
<span class="go">0    5</span>
<span class="go">1    6</span>
<span class="go">2    7</span>
<span class="go">3    8</span>
<span class="go">dtype: int64</span>

<span class="gp">In [261]: </span><span class="n">s</span><span class="o">.</span><span class="n">dt</span><span class="o">.</span><span class="n">components</span>
<span class="gr">Out[261]: </span>
<span class="go">   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds</span>
<span class="go">0     1      0        0        5             0             0            0</span>
<span class="go">1     1      0        0        6             0             0            0</span>
<span class="go">2     1      0        0        7             0             0            0</span>
<span class="go">3     1      0        0        8             0             0            0</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-474">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-475"><code class="docutils literal"><span class="pre">Series.dt</span></code>如果您使用非datetimelike值访问，则会引发<code class="docutils literal"><span class="pre">TypeError</span></code></span></p>
</div>
</div>
<div class="section" id="vectorized-string-methods">
<h2><span class="yiyi-st" id="yiyi-476">Vectorized string methods</span></h2>
<p><span class="yiyi-st" id="yiyi-477">系列配备了一组字符串处理方法，使其易于对数组的每个元素进行操作。</span><span class="yiyi-st" id="yiyi-478">也许最重要的是，这些方法自动排除丢失/ NA值。</span><span class="yiyi-st" id="yiyi-479">这些通过系列的<code class="docutils literal"><span class="pre">str</span></code>属性访问，通常具有与等效（标量）内置字符串方法匹配的名称。</span><span class="yiyi-st" id="yiyi-480">例如：</span></p>
<blockquote>
<div><div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [262]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">&apos;A&apos;</span><span class="p">,</span> <span class="s1">&apos;B&apos;</span><span class="p">,</span> <span class="s1">&apos;C&apos;</span><span class="p">,</span> <span class="s1">&apos;Aaba&apos;</span><span class="p">,</span> <span class="s1">&apos;Baca&apos;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="s1">&apos;CABA&apos;</span><span class="p">,</span> <span class="s1">&apos;dog&apos;</span><span class="p">,</span> <span class="s1">&apos;cat&apos;</span><span class="p">])</span>

<span class="gp">In [263]: </span><span class="n">s</span><span class="o">.</span><span class="n">str</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
<span class="gr">Out[263]: </span>
<span class="go">0       a</span>
<span class="go">1       b</span>
<span class="go">2       c</span>
<span class="go">3    aaba</span>
<span class="go">4    baca</span>
<span class="go">5     NaN</span>
<span class="go">6    caba</span>
<span class="go">7     dog</span>
<span class="go">8     cat</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-481">还提供了强大的模式匹配方法，但请注意，模式匹配通常默认使用<a class="reference external" href="https://docs.python.org/2/library/re.html">正则表达式</a>（在某些情况下总是使用它们）。</span></p>
<p><span class="yiyi-st" id="yiyi-482">有关完整的说明，请参阅<a class="reference internal" href="text.html#text-string-methods"><span class="std std-ref">Vectorized String Methods</span></a>。</span></p>
</div>
<div class="section" id="sorting">
<span id="basics-sorting"></span><h2><span class="yiyi-st" id="yiyi-483">Sorting</span></h2>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-484">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-485">排序API在0.17.0中基本上改变，对于这些改变，参见<a class="reference internal" href="whatsnew.html#whatsnew-0170-api-breaking-sorting"><span class="std std-ref">here</span></a>。</span><span class="yiyi-st" id="yiyi-486">特别地，默认情况下，所有排序方法都返回一个新对象，并且<strong>DO NOT</strong>就地运行（除非传递<code class="docutils literal"><span class="pre">inplace=True</span></code>）。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-487">您可能感兴趣的有两种显而易见的排序：按标签排序和按实际值排序。</span></p>
<div class="section" id="by-index">
<h3><span class="yiyi-st" id="yiyi-488">By Index</span></h3>
<p><span class="yiyi-st" id="yiyi-489">排序轴标签（索引）的主要方法是<code class="docutils literal"><span class="pre">Series.sort_index()</span></code>和<code class="docutils literal"><span class="pre">DataFrame.sort_index()</span></code>方法。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [264]: </span><span class="n">unsorted_df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">reindex</span><span class="p">(</span><span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;d&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">],</span>
<span class="gp">   .....:</span>                          <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;three&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;one&apos;</span><span class="p">])</span>
<span class="gp">   .....:</span> 

<span class="c"># DataFrame</span>
<span class="gp">In [265]: </span><span class="n">unsorted_df</span><span class="o">.</span><span class="n">sort_index</span><span class="p">()</span>
<span class="gr">Out[265]: </span>
<span class="go">   three  two  one</span>
<span class="go">a    NaN  NaN  NaN</span>
<span class="go">b    NaN  NaN  NaN</span>
<span class="go">c    NaN  NaN  NaN</span>
<span class="go">d    NaN  NaN  NaN</span>

<span class="gp">In [266]: </span><span class="n">unsorted_df</span><span class="o">.</span><span class="n">sort_index</span><span class="p">(</span><span class="n">ascending</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[266]: </span>
<span class="go">   three  two  one</span>
<span class="go">d    NaN  NaN  NaN</span>
<span class="go">c    NaN  NaN  NaN</span>
<span class="go">b    NaN  NaN  NaN</span>
<span class="go">a    NaN  NaN  NaN</span>

<span class="gp">In [267]: </span><span class="n">unsorted_df</span><span class="o">.</span><span class="n">sort_index</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[267]: </span>
<span class="go">   one  three  two</span>
<span class="go">a  NaN    NaN  NaN</span>
<span class="go">d  NaN    NaN  NaN</span>
<span class="go">c  NaN    NaN  NaN</span>
<span class="go">b  NaN    NaN  NaN</span>

<span class="c"># Series</span>
<span class="gp">In [268]: </span><span class="n">unsorted_df</span><span class="p">[</span><span class="s1">&apos;three&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">sort_index</span><span class="p">()</span>
<span class="gr">Out[268]: </span>
<span class="go">a   NaN</span>
<span class="go">b   NaN</span>
<span class="go">c   NaN</span>
<span class="go">d   NaN</span>
<span class="go">Name: three, dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="by-values">
<h3><span class="yiyi-st" id="yiyi-490">By Values</span></h3>
<p><span class="yiyi-st" id="yiyi-491"><a class="reference internal" href="generated/pandas.Series.sort_values.html#pandas.Series.sort_values" title="pandas.Series.sort_values"><code class="xref py py-meth docutils literal"><span class="pre">Series.sort_values()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.sort_values.html#pandas.DataFrame.sort_values" title="pandas.DataFrame.sort_values"><code class="xref py py-meth docutils literal"><span class="pre">DataFrame.sort_values()</span></code></a>是<strong>value</strong>排序的入口点（即列或行中的值）。</span><span class="yiyi-st" id="yiyi-492"><a class="reference internal" href="generated/pandas.DataFrame.sort_values.html#pandas.DataFrame.sort_values" title="pandas.DataFrame.sort_values"><code class="xref py py-meth docutils literal"><span class="pre">DataFrame.sort_values()</span></code></a>可以为<code class="docutils literal"><span class="pre">axis=0</span></code>接受可选的<code class="docutils literal"><span class="pre">by</span></code>参数，它将使用任意向量或DataFrame的列名称确定排序顺序：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [269]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;one&apos;</span><span class="p">:[</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span><span class="s1">&apos;two&apos;</span><span class="p">:[</span><span class="mi">1</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span><span class="s1">&apos;three&apos;</span><span class="p">:[</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">]})</span>

<span class="gp">In [270]: </span><span class="n">df1</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="s1">&apos;two&apos;</span><span class="p">)</span>
<span class="gr">Out[270]: </span>
<span class="go">   one  three  two</span>
<span class="go">0    2      5    1</span>
<span class="go">2    1      3    2</span>
<span class="go">1    1      4    3</span>
<span class="go">3    1      2    4</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-493"><code class="docutils literal"><span class="pre">by</span></code>参数可以获取列名称列表，例如：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [271]: </span><span class="n">df1</span><span class="p">[[</span><span class="s1">&apos;one&apos;</span><span class="p">,</span> <span class="s1">&apos;two&apos;</span><span class="p">,</span> <span class="s1">&apos;three&apos;</span><span class="p">]]</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;one&apos;</span><span class="p">,</span><span class="s1">&apos;two&apos;</span><span class="p">])</span>
<span class="gr">Out[271]: </span>
<span class="go">   one  two  three</span>
<span class="go">2    1    2      3</span>
<span class="go">1    1    3      4</span>
<span class="go">3    1    4      2</span>
<span class="go">0    2    1      5</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-494">这些方法通过<code class="docutils literal"><span class="pre">na_position</span></code>参数对NA值进行了特殊处理：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [272]: </span><span class="n">s</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

<span class="gp">In [273]: </span><span class="n">s</span><span class="o">.</span><span class="n">sort_values</span><span class="p">()</span>
<span class="gr">Out[273]: </span>
<span class="go">0       A</span>
<span class="go">3    Aaba</span>
<span class="go">1       B</span>
<span class="go">4    Baca</span>
<span class="go">6    CABA</span>
<span class="go">8     cat</span>
<span class="go">7     dog</span>
<span class="go">2     NaN</span>
<span class="go">5     NaN</span>
<span class="go">dtype: object</span>

<span class="gp">In [274]: </span><span class="n">s</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">na_position</span><span class="o">=</span><span class="s1">&apos;first&apos;</span><span class="p">)</span>
<span class="gr">Out[274]: </span>
<span class="go">2     NaN</span>
<span class="go">5     NaN</span>
<span class="go">0       A</span>
<span class="go">3    Aaba</span>
<span class="go">1       B</span>
<span class="go">4    Baca</span>
<span class="go">6    CABA</span>
<span class="go">8     cat</span>
<span class="go">7     dog</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
</div>
<div class="section" id="searchsorted">
<span id="basics-searchsorted"></span><h3><span class="yiyi-st" id="yiyi-495">searchsorted</span></h3>
<p><span class="yiyi-st" id="yiyi-496">Series具有<a class="reference internal" href="generated/pandas.Series.searchsorted.html#pandas.Series.searchsorted" title="pandas.Series.searchsorted"><code class="xref py py-meth docutils literal"><span class="pre">searchsorted()</span></code></a>方法，其工作方式类似于<a class="reference external" href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.searchsorted.html#numpy.ndarray.searchsorted" title="(in NumPy v1.11)"><code class="xref py py-meth docutils literal"><span class="pre">numpy.ndarray.searchsorted()</span></code></a>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [275]: </span><span class="n">ser</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>

<span class="gp">In [276]: </span><span class="n">ser</span><span class="o">.</span><span class="n">searchsorted</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="gr">Out[276]: </span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>

<span class="gp">In [277]: </span><span class="n">ser</span><span class="o">.</span><span class="n">searchsorted</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
<span class="gr">Out[277]: </span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>

<span class="gp">In [278]: </span><span class="n">ser</span><span class="o">.</span><span class="n">searchsorted</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">side</span><span class="o">=</span><span class="s1">&apos;right&apos;</span><span class="p">)</span>
<span class="gr">Out[278]: </span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>

<span class="gp">In [279]: </span><span class="n">ser</span><span class="o">.</span><span class="n">searchsorted</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">side</span><span class="o">=</span><span class="s1">&apos;left&apos;</span><span class="p">)</span>
<span class="gr">Out[279]: </span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>

<span class="gp">In [280]: </span><span class="n">ser</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>

<span class="gp">In [281]: </span><span class="n">ser</span><span class="o">.</span><span class="n">searchsorted</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">sorter</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">ser</span><span class="p">))</span>
<span class="gr">Out[281]: </span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="section" id="smallest-largest-values">
<span id="basics-nsorted"></span><h3><span class="yiyi-st" id="yiyi-497">smallest / largest values</span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-498"><span class="versionmodified">版本0.14.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-499"><code class="docutils literal"><span class="pre">Series</span></code>具有返回最小或最大的<span class="math">n</span>值的<a class="reference internal" href="generated/pandas.Series.nsmallest.html#pandas.Series.nsmallest" title="pandas.Series.nsmallest"><code class="xref py py-meth docutils literal"><span class="pre">nsmallest()</span></code></a>和<a class="reference internal" href="generated/pandas.Series.nlargest.html#pandas.Series.nlargest" title="pandas.Series.nlargest"><code class="xref py py-meth docutils literal"><span class="pre">nlargest()</span></code></a></span><span class="yiyi-st" id="yiyi-500">对于大的<code class="docutils literal"><span class="pre">Series</span></code>，这可以比排序整个系列并在结果上调用<code class="docutils literal"><span class="pre">head(n)</span></code>更快。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [282]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">permutation</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>

<span class="gp">In [283]: </span><span class="n">s</span>
<span class="gr">Out[283]: </span>
<span class="go">0    9</span>
<span class="go">1    8</span>
<span class="go">2    5</span>
<span class="go">3    3</span>
<span class="go">4    6</span>
<span class="go">5    7</span>
<span class="go">6    0</span>
<span class="go">7    2</span>
<span class="go">8    4</span>
<span class="go">9    1</span>
<span class="go">dtype: int64</span>

<span class="gp">In [284]: </span><span class="n">s</span><span class="o">.</span><span class="n">sort_values</span><span class="p">()</span>
<span class="gr">Out[284]: </span>
<span class="go">6    0</span>
<span class="go">9    1</span>
<span class="go">7    2</span>
<span class="go">3    3</span>
<span class="go">8    4</span>
<span class="go">2    5</span>
<span class="go">4    6</span>
<span class="go">5    7</span>
<span class="go">1    8</span>
<span class="go">0    9</span>
<span class="go">dtype: int64</span>

<span class="gp">In [285]: </span><span class="n">s</span><span class="o">.</span><span class="n">nsmallest</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[285]: </span>
<span class="go">6    0</span>
<span class="go">9    1</span>
<span class="go">7    2</span>
<span class="go">dtype: int64</span>

<span class="gp">In [286]: </span><span class="n">s</span><span class="o">.</span><span class="n">nlargest</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[286]: </span>
<span class="go">0    9</span>
<span class="go">1    8</span>
<span class="go">5    7</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-501"><span class="versionmodified">版本0.17.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-502"><code class="docutils literal"><span class="pre">DataFrame</span></code>也具有<code class="docutils literal"><span class="pre">nlargest</span></code>和<code class="docutils literal"><span class="pre">nsmallest</span></code>方法。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [287]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abdceff&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;c&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">,</span> <span class="mf">3.2</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">3.0</span><span class="p">,</span> <span class="mf">4.0</span><span class="p">]})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [288]: </span><span class="n">df</span><span class="o">.</span><span class="n">nlargest</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">)</span>
<span class="gr">Out[288]: </span>
<span class="go">    a  b    c</span>
<span class="go">5  11  f  3.0</span>
<span class="go">3  10  c  3.2</span>
<span class="go">4   8  e  NaN</span>

<span class="gp">In [289]: </span><span class="n">df</span><span class="o">.</span><span class="n">nlargest</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">])</span>
<span class="gr">Out[289]: </span>
<span class="go">    a  b    c</span>
<span class="go">5  11  f  3.0</span>
<span class="go">3  10  c  3.2</span>
<span class="go">4   8  e  NaN</span>
<span class="go">2   1  d  4.0</span>
<span class="go">1  -1  b  2.0</span>
<span class="go">6  -1  f  4.0</span>

<span class="gp">In [290]: </span><span class="n">df</span><span class="o">.</span><span class="n">nsmallest</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="s1">&apos;a&apos;</span><span class="p">)</span>
<span class="gr">Out[290]: </span>
<span class="go">   a  b    c</span>
<span class="go">0 -2  a  1.0</span>
<span class="go">1 -1  b  2.0</span>
<span class="go">6 -1  f  4.0</span>
<span class="go">1 -1  b  2.0</span>
<span class="go">6 -1  f  4.0</span>

<span class="gp">In [291]: </span><span class="n">df</span><span class="o">.</span><span class="n">nsmallest</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;c&apos;</span><span class="p">])</span>
<span class="gr">Out[291]: </span>
<span class="go">   a  b    c</span>
<span class="go">0 -2  a  1.0</span>
<span class="go">1 -1  b  2.0</span>
<span class="go">6 -1  f  4.0</span>
<span class="go">1 -1  b  2.0</span>
<span class="go">6 -1  f  4.0</span>
<span class="go">2  1  d  4.0</span>
<span class="go">4  8  e  NaN</span>
</pre></div>
</div>
</div>
<div class="section" id="sorting-by-a-multi-index-column">
<span id="basics-multi-index-sorting"></span><h3><span class="yiyi-st" id="yiyi-503">Sorting by a multi-index column</span></h3>
<p><span class="yiyi-st" id="yiyi-504">当列为多索引时，必须明确排序，并且通过完全指定<code class="docutils literal"><span class="pre">by</span></code></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [292]: </span><span class="n">df1</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_tuples</span><span class="p">([(</span><span class="s1">&apos;a&apos;</span><span class="p">,</span><span class="s1">&apos;one&apos;</span><span class="p">),(</span><span class="s1">&apos;a&apos;</span><span class="p">,</span><span class="s1">&apos;two&apos;</span><span class="p">),(</span><span class="s1">&apos;b&apos;</span><span class="p">,</span><span class="s1">&apos;three&apos;</span><span class="p">)])</span>

<span class="gp">In [293]: </span><span class="n">df1</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="p">(</span><span class="s1">&apos;a&apos;</span><span class="p">,</span><span class="s1">&apos;two&apos;</span><span class="p">))</span>
<span class="gr">Out[293]: </span>
<span class="go">    a         b</span>
<span class="go">  one two three</span>
<span class="go">3   1   2     4</span>
<span class="go">2   1   3     2</span>
<span class="go">1   1   4     3</span>
<span class="go">0   2   5     1</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="copying">
<h2><span class="yiyi-st" id="yiyi-505">Copying</span></h2>
<p><span class="yiyi-st" id="yiyi-506">pandas对象上的<a class="reference internal" href="generated/pandas.DataFrame.copy.html#pandas.DataFrame.copy" title="pandas.DataFrame.copy"><code class="xref py py-meth docutils literal"><span class="pre">copy()</span></code></a>方法复制基础数据（虽然不是轴索引，因为它们是不可变的），并返回一个新对象。</span><span class="yiyi-st" id="yiyi-507">请注意，<strong>很少需要复制对象</strong>。</span><span class="yiyi-st" id="yiyi-508">例如，只有很少的方法来改变DataFrame <em>就地</em>：</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-509">插入，删除或修改列</span></li>
<li><span class="yiyi-st" id="yiyi-510">分配到<code class="docutils literal"><span class="pre">index</span></code>或<code class="docutils literal"><span class="pre">columns</span></code>属性</span></li>
<li><span class="yiyi-st" id="yiyi-511">对于同质数据，通过<code class="docutils literal"><span class="pre">values</span></code>属性或高级索引直接修改值</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-512">要清楚，pandas方法一般不对原始对象进行修改；几乎所有的方法都返回新的对象，保持原来的对象不变。</span><span class="yiyi-st" id="yiyi-513">如果数据被修改，那是因为你明确这样做了。</span></p>
</div>
<div class="section" id="dtypes">
<span id="basics-dtypes"></span><h2><span class="yiyi-st" id="yiyi-514">dtypes</span></h2>
<p><span class="yiyi-st" id="yiyi-515">The main types stored in pandas objects are <code class="docutils literal"><span class="pre">float</span></code>, <code class="docutils literal"><span class="pre">int</span></code>, <code class="docutils literal"><span class="pre">bool</span></code>, <code class="docutils literal"><span class="pre">datetime64[ns]</span></code> and <code class="docutils literal"><span class="pre">datetime64[ns,</span> <span class="pre">tz]</span></code> (in &gt;= 0.17.0), <code class="docutils literal"><span class="pre">timedelta[ns]</span></code>, <code class="docutils literal"><span class="pre">category</span></code> (in &gt;= 0.15.0), and <code class="docutils literal"><span class="pre">object</span></code>. </span><span class="yiyi-st" id="yiyi-516">此外，这些类型具有项目大小，例如<code class="docutils literal"><span class="pre">int64</span></code>和<code class="docutils literal"><span class="pre">int32</span></code>。</span><span class="yiyi-st" id="yiyi-517">有关<code class="docutils literal"><span class="pre">datetime64 [ns，</span> <span class="pre">tz]</span></code> dtypes的更多详细信息，请参见<a class="reference internal" href="timeseries.html#timeseries-timezone-series"><span class="std std-ref">Series with TZ</span></a></span></p>
<p><span class="yiyi-st" id="yiyi-518">DataFrames的一个方便的<a class="reference internal" href="generated/pandas.DataFrame.dtypes.html#pandas.DataFrame.dtypes" title="pandas.DataFrame.dtypes"><code class="xref py py-attr docutils literal"><span class="pre">dtypes</span></code></a>属性返回具有每列数据类型的Series。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [294]: </span><span class="n">dft</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span>
<span class="gp">   .....:</span>                         <span class="n">B</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
<span class="gp">   .....:</span>                         <span class="n">C</span> <span class="o">=</span> <span class="s1">&apos;foo&apos;</span><span class="p">,</span>
<span class="gp">   .....:</span>                         <span class="n">D</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timestamp</span><span class="p">(</span><span class="s1">&apos;20010102&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                         <span class="n">E</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mf">1.0</span><span class="p">]</span><span class="o">*</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;float32&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                                     <span class="n">F</span> <span class="o">=</span> <span class="bp">False</span><span class="p">,</span>
<span class="gp">   .....:</span>                                     <span class="n">G</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="mi">3</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;int8&apos;</span><span class="p">)))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [295]: </span><span class="n">dft</span>
<span class="gr">Out[295]: </span>
<span class="go">          A  B    C          D    E      F  G</span>
<span class="go">0  0.954940  1  foo 2001-01-02  1.0  False  1</span>
<span class="go">1  0.318163  1  foo 2001-01-02  1.0  False  1</span>
<span class="go">2  0.985803  1  foo 2001-01-02  1.0  False  1</span>

<span class="gp">In [296]: </span><span class="n">dft</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[296]: </span>
<span class="go">A           float64</span>
<span class="go">B             int64</span>
<span class="go">C            object</span>
<span class="go">D    datetime64[ns]</span>
<span class="go">E           float32</span>
<span class="go">F              bool</span>
<span class="go">G              int8</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-519">在<code class="docutils literal"><span class="pre">Series</span></code>上使用<a class="reference internal" href="generated/pandas.Series.dtype.html#pandas.Series.dtype" title="pandas.Series.dtype"><code class="xref py py-attr docutils literal"><span class="pre">dtype</span></code></a>属性。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [297]: </span><span class="n">dft</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">dtype</span>
<span class="gr">Out[297]: </span><span class="n">dtype</span><span class="p">(</span><span class="s1">&apos;float64&apos;</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-520">如果pandas对象包含多个数据类型<em>IN A SINGLE COLUMN</em>，则将选择列的dtype以适应所有数据类型（<code class="docutils literal"><span class="pre">object</span></code>是最常见的）。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># these ints are coerced to floats</span>
<span class="gp">In [298]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mf">6.</span><span class="p">])</span>
<span class="gr">Out[298]: </span>
<span class="go">0    1.0</span>
<span class="go">1    2.0</span>
<span class="go">2    3.0</span>
<span class="go">3    4.0</span>
<span class="go">4    5.0</span>
<span class="go">5    6.0</span>
<span class="go">dtype: float64</span>

<span class="c"># string data forces an ``object`` dtype</span>
<span class="gp">In [299]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mf">6.</span><span class="p">,</span> <span class="s1">&apos;foo&apos;</span><span class="p">])</span>
<span class="gr">Out[299]: </span>
<span class="go">0      1</span>
<span class="go">1      2</span>
<span class="go">2      3</span>
<span class="go">3      6</span>
<span class="go">4    foo</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-521">方法<a class="reference internal" href="generated/pandas.DataFrame.get_dtype_counts.html#pandas.DataFrame.get_dtype_counts" title="pandas.DataFrame.get_dtype_counts"><code class="xref py py-meth docutils literal"><span class="pre">get_dtype_counts()</span></code></a>将返回<code class="docutils literal"><span class="pre">DataFrame</span></code>中每种类型的列数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [300]: </span><span class="n">dft</span><span class="o">.</span><span class="n">get_dtype_counts</span><span class="p">()</span>
<span class="gr">Out[300]: </span>
<span class="go">bool              1</span>
<span class="go">datetime64[ns]    1</span>
<span class="go">float32           1</span>
<span class="go">float64           1</span>
<span class="go">int64             1</span>
<span class="go">int8              1</span>
<span class="go">object            1</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-522">数字类型将传播并且可以在DataFrames中共存（从v0.11.0开始）。</span><span class="yiyi-st" id="yiyi-523">如果传递一个dtype（直接通过<code class="docutils literal"><span class="pre">dtype</span></code>关键字，传递<code class="docutils literal"><span class="pre">ndarray</span></code>或传递<code class="docutils literal"><span class="pre">Series</span></code>，它将被保留在DataFrame操作。</span><span class="yiyi-st" id="yiyi-524">此外，不同的数字类型将<strong>NOT</strong>组合。</span><span class="yiyi-st" id="yiyi-525">下面的例子会给你一个味道。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [301]: </span><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;float32&apos;</span><span class="p">)</span>

<span class="gp">In [302]: </span><span class="n">df1</span>
<span class="gr">Out[302]: </span>
<span class="go">          A</span>
<span class="go">0  0.647650</span>
<span class="go">1  0.822993</span>
<span class="go">2  1.778703</span>
<span class="go">3 -1.543048</span>
<span class="go">4 -0.123256</span>
<span class="go">5  2.239740</span>
<span class="go">6 -0.143778</span>
<span class="go">7 -2.885090</span>

<span class="gp">In [303]: </span><span class="n">df1</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[303]: </span>
<span class="go">A    float32</span>
<span class="go">dtype: object</span>

<span class="gp">In [304]: </span><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span> <span class="n">A</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;float16&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                         <span class="n">B</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">)),</span>
<span class="gp">   .....:</span>                         <span class="n">C</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;uint8&apos;</span><span class="p">))</span> <span class="p">))</span>
<span class="gp">   .....:</span> 

<span class="gp">In [305]: </span><span class="n">df2</span>
<span class="gr">Out[305]: </span>
<span class="go">          A         B    C</span>
<span class="go">0  0.027588  0.296947    0</span>
<span class="go">1 -1.150391  0.007045  255</span>
<span class="go">2  0.246460  0.707877    1</span>
<span class="go">3 -0.455078  0.950661    0</span>
<span class="go">4 -1.507812  0.087527    0</span>
<span class="go">5 -0.502441 -0.339212    0</span>
<span class="go">6  0.528809 -0.278698    0</span>
<span class="go">7  0.590332  1.775379    0</span>

<span class="gp">In [306]: </span><span class="n">df2</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[306]: </span>
<span class="go">A    float16</span>
<span class="go">B    float64</span>
<span class="go">C      uint8</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<div class="section" id="defaults">
<h3><span class="yiyi-st" id="yiyi-526">defaults</span></h3>
<p><span class="yiyi-st" id="yiyi-527">默认整数类型为<code class="docutils literal"><span class="pre">int64</span></code>，float类型为平台（32位或64位）的<code class="docutils literal"><span class="pre">float64</span></code>，<em>REGARDLESS</em>。</span><span class="yiyi-st" id="yiyi-528">以下都将导致<code class="docutils literal"><span class="pre">int64</span></code> dtypes。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [307]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">])</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[307]: </span>
<span class="go">a    int64</span>
<span class="go">dtype: object</span>

<span class="gp">In [308]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]})</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[308]: </span>
<span class="go">a    int64</span>
<span class="go">dtype: object</span>

<span class="gp">In [309]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="mi">1</span> <span class="p">},</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">)))</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[309]: </span>
<span class="go">a    int64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-529">Numpy，但是会在创建数组时选择<em>平台相关</em>类型。</span><span class="yiyi-st" id="yiyi-530">以下<strong>WILL</strong>会导致32位平台上的<code class="docutils literal"><span class="pre">int32</span></code>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [310]: </span><span class="n">frame</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]))</span>
</pre></div>
</div>
</div>
<div class="section" id="upcasting">
<h3><span class="yiyi-st" id="yiyi-531">upcasting</span></h3>
<p><span class="yiyi-st" id="yiyi-532">当与其他类型组合时，类型可以被<em>向上转换</em>，这意味着它们从当前类型（例如<code class="docutils literal"><span class="pre">int</span></code>提升到<code class="docutils literal"><span class="pre">float</span></code>）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [311]: </span><span class="n">df3</span> <span class="o">=</span> <span class="n">df1</span><span class="o">.</span><span class="n">reindex_like</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span> <span class="o">+</span> <span class="n">df2</span>

<span class="gp">In [312]: </span><span class="n">df3</span>
<span class="gr">Out[312]: </span>
<span class="go">          A         B      C</span>
<span class="go">0  0.675238  0.296947    0.0</span>
<span class="go">1 -0.327398  0.007045  255.0</span>
<span class="go">2  2.025163  0.707877    1.0</span>
<span class="go">3 -1.998126  0.950661    0.0</span>
<span class="go">4 -1.631068  0.087527    0.0</span>
<span class="go">5  1.737299 -0.339212    0.0</span>
<span class="go">6  0.385030 -0.278698    0.0</span>
<span class="go">7 -2.294758  1.775379    0.0</span>

<span class="gp">In [313]: </span><span class="n">df3</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[313]: </span>
<span class="go">A    float32</span>
<span class="go">B    float64</span>
<span class="go">C    float64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-533">DataFrame上的<code class="docutils literal"><span class="pre">values</span></code>属性返回dtypes的<em>低共同分母</em>，表示可以适应<strong>所有</strong>类型的dtype得到均匀的数字numpy数组。</span><span class="yiyi-st" id="yiyi-534">这可能会强制某些<em>向上转换</em>。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [314]: </span><span class="n">df3</span><span class="o">.</span><span class="n">values</span><span class="o">.</span><span class="n">dtype</span>
<span class="gr">Out[314]: </span><span class="n">dtype</span><span class="p">(</span><span class="s1">&apos;float64&apos;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="astype">
<h3><span class="yiyi-st" id="yiyi-535">astype</span></h3>
<p id="basics-cast"><span class="yiyi-st" id="yiyi-536">您可以使用<a class="reference internal" href="generated/pandas.DataFrame.astype.html#pandas.DataFrame.astype" title="pandas.DataFrame.astype"><code class="xref py py-meth docutils literal"><span class="pre">astype()</span></code></a>方法将dtypes从一个显式转换为另一个。</span><span class="yiyi-st" id="yiyi-537">这些将默认返回一个副本，即使dtype没有改变（通过<code class="docutils literal"><span class="pre">copy=False</span></code>来改变这种行为）。</span><span class="yiyi-st" id="yiyi-538">此外，如果astype操作无效，它们将引发异常。</span></p>
<p><span class="yiyi-st" id="yiyi-539">向上转换总是根据<strong>numpy</strong>规则。</span><span class="yiyi-st" id="yiyi-540">如果在操作中涉及两个不同的类型，则更多的<em>一般</em>将被用作操作的结果。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [315]: </span><span class="n">df3</span>
<span class="gr">Out[315]: </span>
<span class="go">          A         B      C</span>
<span class="go">0  0.675238  0.296947    0.0</span>
<span class="go">1 -0.327398  0.007045  255.0</span>
<span class="go">2  2.025163  0.707877    1.0</span>
<span class="go">3 -1.998126  0.950661    0.0</span>
<span class="go">4 -1.631068  0.087527    0.0</span>
<span class="go">5  1.737299 -0.339212    0.0</span>
<span class="go">6  0.385030 -0.278698    0.0</span>
<span class="go">7 -2.294758  1.775379    0.0</span>

<span class="gp">In [316]: </span><span class="n">df3</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[316]: </span>
<span class="go">A    float32</span>
<span class="go">B    float64</span>
<span class="go">C    float64</span>
<span class="go">dtype: object</span>

<span class="c"># conversion of dtypes</span>
<span class="gp">In [317]: </span><span class="n">df3</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;float32&apos;</span><span class="p">)</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[317]: </span>
<span class="go">A    float32</span>
<span class="go">B    float32</span>
<span class="go">C    float32</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-541">使用<a class="reference internal" href="generated/pandas.DataFrame.astype.html#pandas.DataFrame.astype" title="pandas.DataFrame.astype"><code class="xref py py-meth docutils literal"><span class="pre">astype()</span></code></a>将列的一个子集转换为指定的类型</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [318]: </span><span class="n">dft</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">],</span> <span class="s1">&apos;c&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]})</span>

<span class="gp">In [319]: </span><span class="n">dft</span><span class="p">[[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span><span class="s1">&apos;b&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="n">dft</span><span class="p">[[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span><span class="s1">&apos;b&apos;</span><span class="p">]]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">)</span>

<span class="gp">In [320]: </span><span class="n">dft</span>
<span class="gr">Out[320]: </span>
<span class="go">   a  b  c</span>
<span class="go">0  1  4  7</span>
<span class="go">1  2  5  8</span>
<span class="go">2  3  6  9</span>

<span class="gp">In [321]: </span><span class="n">dft</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[321]: </span>
<span class="go">a    uint8</span>
<span class="go">b    uint8</span>
<span class="go">c    int64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-542">注意</span></p>
<p><span class="yiyi-st" id="yiyi-543">当尝试使用<a class="reference internal" href="generated/pandas.DataFrame.astype.html#pandas.DataFrame.astype" title="pandas.DataFrame.astype"><code class="xref py py-meth docutils literal"><span class="pre">astype()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.loc.html#pandas.DataFrame.loc" title="pandas.DataFrame.loc"><code class="xref py py-meth docutils literal"><span class="pre">loc()</span></code></a>将列子集转换为指定类型时，将发生向上转换。</span></p>
<p><span class="yiyi-st" id="yiyi-544"><a class="reference internal" href="generated/pandas.DataFrame.loc.html#pandas.DataFrame.loc" title="pandas.DataFrame.loc"><code class="xref py py-meth docutils literal"><span class="pre">loc()</span></code></a>尝试适合我们分配给当前的dtypes，而<code class="docutils literal"><span class="pre">[]</span></code>将从右侧覆盖它们。</span><span class="yiyi-st" id="yiyi-545">因此，下面的代码段会产生非预期的结果。</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [322]: </span><span class="n">dft</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;a&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="s1">&apos;b&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">],</span> <span class="s1">&apos;c&apos;</span><span class="p">:</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]})</span>

<span class="gp">In [323]: </span><span class="n">dft</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">]]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">)</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[323]: </span>
<span class="go">a    uint8</span>
<span class="go">b    uint8</span>
<span class="go">dtype: object</span>

<span class="gp">In [324]: </span><span class="n">dft</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">]]</span> <span class="o">=</span> <span class="n">dft</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="p">[</span><span class="s1">&apos;a&apos;</span><span class="p">,</span> <span class="s1">&apos;b&apos;</span><span class="p">]]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">)</span>

<span class="gp">In [325]: </span><span class="n">dft</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[325]: </span>
<span class="go">a    int64</span>
<span class="go">b    int64</span>
<span class="go">c    int64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="object-conversion">
<span id="basics-object-conversion"></span><h3><span class="yiyi-st" id="yiyi-546">object conversion</span></h3>
<p><span class="yiyi-st" id="yiyi-547">pandas提供了各种函数来尝试强制将类型从<code class="docutils literal"><span class="pre">object</span></code> dtype转换为其他类型。</span><span class="yiyi-st" id="yiyi-548">以下函数可用于一维对象数组或标量：</span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-549"><a class="reference internal" href="generated/pandas.to_numeric.html#pandas.to_numeric" title="pandas.to_numeric"><code class="xref py py-meth docutils literal"><span class="pre">to_numeric()</span></code></a>（转换为数字dtypes）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [326]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;1.1&apos;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>

<span class="gp">In [327]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="gr">Out[327]: </span><span class="n">array</span><span class="p">([</span> <span class="mf">1.1</span><span class="p">,</span>  <span class="mf">2.</span> <span class="p">,</span>  <span class="mf">3.</span> <span class="p">])</span>
</pre></div>
</div>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-550"><a class="reference internal" href="generated/pandas.to_datetime.html#pandas.to_datetime" title="pandas.to_datetime"><code class="xref py py-meth docutils literal"><span class="pre">to_datetime()</span></code></a>（转换为datetime对象）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [328]: </span><span class="kn">import</span> <span class="nn">datetime</span>

<span class="gp">In [329]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;2016-07-09&apos;</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)]</span>

<span class="gp">In [330]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="gr">Out[330]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;2016-07-09&apos;</span><span class="p">,</span> <span class="s1">&apos;2016-03-02&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-551"><a class="reference internal" href="generated/pandas.to_timedelta.html#pandas.to_timedelta" title="pandas.to_timedelta"><code class="xref py py-meth docutils literal"><span class="pre">to_timedelta()</span></code></a>（转换为timedelta对象）</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [331]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;5us&apos;</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">&apos;1day&apos;</span><span class="p">)]</span>

<span class="gp">In [332]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_timedelta</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="gr">Out[332]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="s1">&apos;0 days 00:00:00.000005&apos;</span><span class="p">,</span> <span class="s1">&apos;1 days 00:00:00&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;timedelta64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-552">要强制执行转换，我们可以传递<code class="docutils literal"><span class="pre">errors</span></code>参数，它指定了pandas应该如何处理无法转换为所需dtype或对象的元素。</span><span class="yiyi-st" id="yiyi-553">默认情况下，<code class="docutils literal"><span class="pre">errors=&apos;raise&apos;</span></code>，表示在转换过程中会遇到任何错误。</span><span class="yiyi-st" id="yiyi-554">但是，如果<code class="docutils literal"><span class="pre">errors=&apos;coerce&apos;</span></code>，这些错误将被忽略，pandas会将有问题的元素转换为<code class="docutils literal"><span class="pre">pd.NaT</span></code>（datetime和timedelta）或<code class="docutils literal"><span class="pre">np.nan</span></code>（用于数字）。</span><span class="yiyi-st" id="yiyi-555">这可能是有用的，如果你在读取大多数所需的dtype（例如，数字，datetime）的数据，但偶尔有不合格的元素混合，你想表示为丢失：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [333]: </span><span class="kn">import</span> <span class="nn">datetime</span>

<span class="gp">In [334]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)]</span>

<span class="gp">In [335]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;coerce&apos;</span><span class="p">)</span>
<span class="gr">Out[335]: </span><span class="n">DatetimeIndex</span><span class="p">([</span><span class="s1">&apos;NaT&apos;</span><span class="p">,</span> <span class="s1">&apos;2016-03-02&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;datetime64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

<span class="gp">In [336]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>

<span class="gp">In [337]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;coerce&apos;</span><span class="p">)</span>
<span class="gr">Out[337]: </span><span class="n">array</span><span class="p">([</span> <span class="n">nan</span><span class="p">,</span>   <span class="mf">2.</span><span class="p">,</span>   <span class="mf">3.</span><span class="p">])</span>

<span class="gp">In [338]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">&apos;1day&apos;</span><span class="p">)]</span>

<span class="gp">In [339]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_timedelta</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;coerce&apos;</span><span class="p">)</span>
<span class="gr">Out[339]: </span><span class="n">TimedeltaIndex</span><span class="p">([</span><span class="n">NaT</span><span class="p">,</span> <span class="s1">&apos;1 days&apos;</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;timedelta64[ns]&apos;</span><span class="p">,</span> <span class="n">freq</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-556"><code class="docutils literal"><span class="pre">errors</span></code>参数有第三个选项<code class="docutils literal"><span class="pre">errors=&apos;ignore&apos;</span></code>，如果遇到转换为所需数据类型的任何错误，它将简单地返回传入的数据：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [340]: </span><span class="kn">import</span> <span class="nn">datetime</span>

<span class="gp">In [341]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)]</span>

<span class="gp">In [342]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;ignore&apos;</span><span class="p">)</span>
<span class="gr">Out[342]: </span><span class="n">array</span><span class="p">([</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>

<span class="gp">In [343]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>

<span class="gp">In [344]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;ignore&apos;</span><span class="p">)</span>
<span class="gr">Out[344]: </span><span class="n">array</span><span class="p">([</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>

<span class="gp">In [345]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">&apos;1day&apos;</span><span class="p">)]</span>

<span class="gp">In [346]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_timedelta</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="s1">&apos;ignore&apos;</span><span class="p">)</span>
<span class="gr">Out[346]: </span><span class="n">array</span><span class="p">([</span><span class="s1">&apos;apple&apos;</span><span class="p">,</span> <span class="n">Timedelta</span><span class="p">(</span><span class="s1">&apos;1 days 00:00:00&apos;</span><span class="p">)],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">object</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-557">除了对象转换，<a class="reference internal" href="generated/pandas.to_numeric.html#pandas.to_numeric" title="pandas.to_numeric"><code class="xref py py-meth docutils literal"><span class="pre">to_numeric()</span></code></a>提供了另一个参数<code class="docutils literal"><span class="pre">downcast</span></code>，它提供了将新（或已经）数字数据下转换为较小的dtype的选项，记忆：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [347]: </span><span class="n">m</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&apos;1&apos;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>

<span class="gp">In [348]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">downcast</span><span class="o">=</span><span class="s1">&apos;integer&apos;</span><span class="p">)</span>   <span class="c1"># smallest signed int dtype</span>
<span class="gr">Out[348]: </span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">int8</span><span class="p">)</span>

<span class="gp">In [349]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">downcast</span><span class="o">=</span><span class="s1">&apos;signed&apos;</span><span class="p">)</span>    <span class="c1"># same as &apos;integer&apos;</span>
<span class="gr">Out[349]: </span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">int8</span><span class="p">)</span>

<span class="gp">In [350]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">downcast</span><span class="o">=</span><span class="s1">&apos;unsigned&apos;</span><span class="p">)</span>  <span class="c1"># smallest unsigned int dtype</span>
<span class="gr">Out[350]: </span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">uint8</span><span class="p">)</span>

<span class="gp">In [351]: </span><span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="n">downcast</span><span class="o">=</span><span class="s1">&apos;float&apos;</span><span class="p">)</span>     <span class="c1"># smallest float dtype</span>
<span class="gr">Out[351]: </span><span class="n">array</span><span class="p">([</span> <span class="mf">1.</span><span class="p">,</span>  <span class="mf">2.</span><span class="p">,</span>  <span class="mf">3.</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">float32</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-558">因为这些方法仅适用于一维数组，列表或标量；它们不能直接用于多维对象，如DataFrames。</span><span class="yiyi-st" id="yiyi-559">然而，使用<a class="reference internal" href="generated/pandas.DataFrame.apply.html#pandas.DataFrame.apply" title="pandas.DataFrame.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>，我们可以有效地“应用”每个列上的函数：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [352]: </span><span class="kn">import</span> <span class="nn">datetime</span>

<span class="gp">In [353]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([[</span><span class="s1">&apos;2016-07-09&apos;</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2016</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">)]]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;O&apos;</span><span class="p">)</span>

<span class="gp">In [354]: </span><span class="n">df</span>
<span class="gr">Out[354]: </span>
<span class="go">            0                    1</span>
<span class="go">0  2016-07-09  2016-03-02 00:00:00</span>
<span class="go">1  2016-07-09  2016-03-02 00:00:00</span>

<span class="gp">In [355]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">to_datetime</span><span class="p">)</span>
<span class="gr">Out[355]: </span>
<span class="go">           0          1</span>
<span class="go">0 2016-07-09 2016-03-02</span>
<span class="go">1 2016-07-09 2016-03-02</span>

<span class="gp">In [356]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([[</span><span class="s1">&apos;1.1&apos;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;O&apos;</span><span class="p">)</span>

<span class="gp">In [357]: </span><span class="n">df</span>
<span class="gr">Out[357]: </span>
<span class="go">     0  1  2</span>
<span class="go">0  1.1  2  3</span>
<span class="go">1  1.1  2  3</span>

<span class="gp">In [358]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">to_numeric</span><span class="p">)</span>
<span class="gr">Out[358]: </span>
<span class="go">     0  1  2</span>
<span class="go">0  1.1  2  3</span>
<span class="go">1  1.1  2  3</span>

<span class="gp">In [359]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([[</span><span class="s1">&apos;5us&apos;</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">Timedelta</span><span class="p">(</span><span class="s1">&apos;1day&apos;</span><span class="p">)]]</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&apos;O&apos;</span><span class="p">)</span>

<span class="gp">In [360]: </span><span class="n">df</span>
<span class="gr">Out[360]: </span>
<span class="go">     0                1</span>
<span class="go">0  5us  1 days 00:00:00</span>
<span class="go">1  5us  1 days 00:00:00</span>

<span class="gp">In [361]: </span><span class="n">df</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">to_timedelta</span><span class="p">)</span>
<span class="gr">Out[361]: </span>
<span class="go">                0      1</span>
<span class="go">0 00:00:00.000005 1 days</span>
<span class="go">1 00:00:00.000005 1 days</span>
</pre></div>
</div>
</div>
<div class="section" id="gotchas">
<h3><span class="yiyi-st" id="yiyi-560">gotchas</span></h3>
<p><span class="yiyi-st" id="yiyi-561">在<code class="docutils literal"><span class="pre">integer</span></code>类型数据上执行选择操作可以轻松地将数据上传到<code class="docutils literal"><span class="pre">floating</span></code>。</span><span class="yiyi-st" id="yiyi-562">在未引入<code class="docutils literal"><span class="pre">nans</span></code>（从0.11.0开始）的情况下，输入数据的dtype将被保留。参见<a class="reference internal" href="gotchas.html#gotchas-intna"><span class="std std-ref">integer na gotchas</span></a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [362]: </span><span class="n">dfi</span> <span class="o">=</span> <span class="n">df3</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;int32&apos;</span><span class="p">)</span>

<span class="gp">In [363]: </span><span class="n">dfi</span><span class="p">[</span><span class="s1">&apos;E&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>

<span class="gp">In [364]: </span><span class="n">dfi</span>
<span class="gr">Out[364]: </span>
<span class="go">   A  B    C  E</span>
<span class="go">0  0  0    0  1</span>
<span class="go">1  0  0  255  1</span>
<span class="go">2  2  0    1  1</span>
<span class="go">3 -1  0    0  1</span>
<span class="go">4 -1  0    0  1</span>
<span class="go">5  1  0    0  1</span>
<span class="go">6  0  0    0  1</span>
<span class="go">7 -2  1    0  1</span>

<span class="gp">In [365]: </span><span class="n">dfi</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[365]: </span>
<span class="go">A    int32</span>
<span class="go">B    int32</span>
<span class="go">C    int32</span>
<span class="go">E    int64</span>
<span class="go">dtype: object</span>

<span class="gp">In [366]: </span><span class="n">casted</span> <span class="o">=</span> <span class="n">dfi</span><span class="p">[</span><span class="n">dfi</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">]</span>

<span class="gp">In [367]: </span><span class="n">casted</span>
<span class="gr">Out[367]: </span>
<span class="go">     A    B      C  E</span>
<span class="go">0  NaN  NaN    NaN  1</span>
<span class="go">1  NaN  NaN  255.0  1</span>
<span class="go">2  2.0  NaN    1.0  1</span>
<span class="go">3  NaN  NaN    NaN  1</span>
<span class="go">4  NaN  NaN    NaN  1</span>
<span class="go">5  1.0  NaN    NaN  1</span>
<span class="go">6  NaN  NaN    NaN  1</span>
<span class="go">7  NaN  1.0    NaN  1</span>

<span class="gp">In [368]: </span><span class="n">casted</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[368]: </span>
<span class="go">A    float64</span>
<span class="go">B    float64</span>
<span class="go">C    float64</span>
<span class="go">E      int64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-563">而float dtypes不变。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [369]: </span><span class="n">dfa</span> <span class="o">=</span> <span class="n">df3</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

<span class="gp">In [370]: </span><span class="n">dfa</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">dfa</span><span class="p">[</span><span class="s1">&apos;A&apos;</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;float32&apos;</span><span class="p">)</span>

<span class="gp">In [371]: </span><span class="n">dfa</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[371]: </span>
<span class="go">A    float32</span>
<span class="go">B    float64</span>
<span class="go">C    float64</span>
<span class="go">dtype: object</span>

<span class="gp">In [372]: </span><span class="n">casted</span> <span class="o">=</span> <span class="n">dfa</span><span class="p">[</span><span class="n">df2</span><span class="o">&gt;</span><span class="mi">0</span><span class="p">]</span>

<span class="gp">In [373]: </span><span class="n">casted</span>
<span class="gr">Out[373]: </span>
<span class="go">          A         B      C</span>
<span class="go">0  0.675238  0.296947    NaN</span>
<span class="go">1       NaN  0.007045  255.0</span>
<span class="go">2  2.025163  0.707877    1.0</span>
<span class="go">3       NaN  0.950661    NaN</span>
<span class="go">4       NaN  0.087527    NaN</span>
<span class="go">5       NaN       NaN    NaN</span>
<span class="go">6  0.385030       NaN    NaN</span>
<span class="go">7 -2.294758  1.775379    NaN</span>

<span class="gp">In [374]: </span><span class="n">casted</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[374]: </span>
<span class="go">A    float32</span>
<span class="go">B    float64</span>
<span class="go">C    float64</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="selecting-columns-based-on-dtype">
<h2><span class="yiyi-st" id="yiyi-564">Selecting columns based on <code class="docutils literal"><span class="pre">dtype</span></code></span></h2>
<div class="versionadded" id="basics-selectdtypes">
<p><span class="yiyi-st" id="yiyi-565"><span class="versionmodified">版本0.14.1中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-566"><a class="reference internal" href="generated/pandas.DataFrame.select_dtypes.html#pandas.DataFrame.select_dtypes" title="pandas.DataFrame.select_dtypes"><code class="xref py py-meth docutils literal"><span class="pre">select_dtypes()</span></code></a>方法基于其<code class="docutils literal"><span class="pre">dtype</span></code>实现列的子集。</span></p>
<p><span class="yiyi-st" id="yiyi-567">首先，让我们创建一个具有不同dtypes的<a class="reference internal" href="generated/pandas.DataFrame.html#pandas.DataFrame" title="pandas.DataFrame"><code class="xref py py-class docutils literal"><span class="pre">DataFrame</span></code></a>：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [375]: </span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&apos;string&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="s1">&apos;abc&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;int64&apos;</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">)),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;uint8&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;u1&apos;</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;float64&apos;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">4.0</span><span class="p">,</span> <span class="mf">7.0</span><span class="p">),</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;bool1&apos;</span><span class="p">:</span> <span class="p">[</span><span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">True</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;bool2&apos;</span><span class="p">:</span> <span class="p">[</span><span class="bp">False</span><span class="p">,</span> <span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">],</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;dates&apos;</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;now&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">,</span>
<span class="gp">   .....:</span>                    <span class="s1">&apos;category&apos;</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="s2">&quot;ABC&quot;</span><span class="p">))</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;category&apos;</span><span class="p">)})</span>
<span class="gp">   .....:</span> 

<span class="gp">In [376]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;tdeltas&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">dates</span><span class="o">.</span><span class="n">diff</span><span class="p">()</span>

<span class="gp">In [377]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;uint64&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&apos;u8&apos;</span><span class="p">)</span>

<span class="gp">In [378]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;other_dates&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">values</span>

<span class="gp">In [379]: </span><span class="n">df</span><span class="p">[</span><span class="s1">&apos;tz_aware_dates&apos;</span><span class="p">]</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">&apos;20130101&apos;</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">tz</span><span class="o">=</span><span class="s1">&apos;US/Eastern&apos;</span><span class="p">)</span>

<span class="gp">In [380]: </span><span class="n">df</span>
<span class="gr">Out[380]: </span>
<span class="go">   bool1  bool2 category                      dates  float64  int64 string  \</span>
<span class="go">0   True  False        A 2016-12-24 18:31:36.297875      4.0      1      a   </span>
<span class="go">1  False   True        B 2016-12-25 18:31:36.297875      5.0      2      b   </span>
<span class="go">2   True  False        C 2016-12-26 18:31:36.297875      6.0      3      c   </span>

<span class="go">   uint8  tdeltas  uint64 other_dates            tz_aware_dates  </span>
<span class="go">0      3      NaT       3  2013-01-01 2013-01-01 00:00:00-05:00  </span>
<span class="go">1      4   1 days       4  2013-01-02 2013-01-02 00:00:00-05:00  </span>
<span class="go">2      5   1 days       5  2013-01-03 2013-01-03 00:00:00-05:00  </span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-568">和dtypes</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [381]: </span><span class="n">df</span><span class="o">.</span><span class="n">dtypes</span>
<span class="gr">Out[381]: </span>
<span class="go">bool1                                   bool</span>
<span class="go">bool2                                   bool</span>
<span class="go">category                            category</span>
<span class="go">dates                         datetime64[ns]</span>
<span class="go">float64                              float64</span>
<span class="go">int64                                  int64</span>
<span class="go">string                                object</span>
<span class="go">uint8                                  uint8</span>
<span class="go">tdeltas                      timedelta64[ns]</span>
<span class="go">uint64                                uint64</span>
<span class="go">other_dates                   datetime64[ns]</span>
<span class="go">tz_aware_dates    datetime64[ns, US/Eastern]</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-569"><a class="reference internal" href="generated/pandas.DataFrame.select_dtypes.html#pandas.DataFrame.select_dtypes" title="pandas.DataFrame.select_dtypes"><code class="xref py py-meth docutils literal"><span class="pre">select_dtypes()</span></code></a> has two parameters <code class="docutils literal"><span class="pre">include</span></code> and <code class="docutils literal"><span class="pre">exclude</span></code> that allow you to say “give me the columns WITH these dtypes” (<code class="docutils literal"><span class="pre">include</span></code>) and/or “give the columns WITHOUT these dtypes” (<code class="docutils literal"><span class="pre">exclude</span></code>).</span></p>
<p><span class="yiyi-st" id="yiyi-570">例如，要选择<code class="docutils literal"><span class="pre">bool</span></code>列</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [382]: </span><span class="n">df</span><span class="o">.</span><span class="n">select_dtypes</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="nb">bool</span><span class="p">])</span>
<span class="gr">Out[382]: </span>
<span class="go">   bool1  bool2</span>
<span class="go">0   True  False</span>
<span class="go">1  False   True</span>
<span class="go">2   True  False</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-571">您还可以在<a class="reference external" href="http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html">numpy dtype层次结构</a>中传递dtype的名称：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [383]: </span><span class="n">df</span><span class="o">.</span><span class="n">select_dtypes</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;bool&apos;</span><span class="p">])</span>
<span class="gr">Out[383]: </span>
<span class="go">   bool1  bool2</span>
<span class="go">0   True  False</span>
<span class="go">1  False   True</span>
<span class="go">2   True  False</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-572"><a class="reference internal" href="generated/pandas.DataFrame.select_dtypes.html#pandas.DataFrame.select_dtypes" title="pandas.DataFrame.select_dtypes"><code class="xref py py-meth docutils literal"><span class="pre">select_dtypes()</span></code></a>也适用于通用dtypes。</span></p>
<p><span class="yiyi-st" id="yiyi-573">例如，要选择所有数字和布尔列，同时排除无符号整数</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [384]: </span><span class="n">df</span><span class="o">.</span><span class="n">select_dtypes</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;number&apos;</span><span class="p">,</span> <span class="s1">&apos;bool&apos;</span><span class="p">],</span> <span class="n">exclude</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;unsignedinteger&apos;</span><span class="p">])</span>
<span class="gr">Out[384]: </span>
<span class="go">   bool1  bool2  float64  int64  tdeltas</span>
<span class="go">0   True  False      4.0      1      NaT</span>
<span class="go">1  False   True      5.0      2   1 days</span>
<span class="go">2   True  False      6.0      3   1 days</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-574">要选择字符串列，必须使用<code class="docutils literal"><span class="pre">object</span></code> dtype：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [385]: </span><span class="n">df</span><span class="o">.</span><span class="n">select_dtypes</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="s1">&apos;object&apos;</span><span class="p">])</span>
<span class="gr">Out[385]: </span>
<span class="go">  string</span>
<span class="go">0      a</span>
<span class="go">1      b</span>
<span class="go">2      c</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-575">要查看像<code class="docutils literal"><span class="pre">numpy.number</span></code>这样的通用<code class="docutils literal"><span class="pre">dtype</span></code>的所有子类型，您可以定义一个函数，返回子类型的树：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [386]: </span><span class="k">def</span> <span class="nf">subdtypes</span><span class="p">(</span><span class="n">dtype</span><span class="p">):</span>
<span class="gp">   .....:</span>     <span class="n">subs</span> <span class="o">=</span> <span class="n">dtype</span><span class="o">.</span><span class="n">__subclasses__</span><span class="p">()</span>
<span class="gp">   .....:</span>     <span class="k">if</span> <span class="ow">not</span> <span class="n">subs</span><span class="p">:</span>
<span class="gp">   .....:</span>         <span class="k">return</span> <span class="n">dtype</span>
<span class="gp">   .....:</span>     <span class="k">return</span> <span class="p">[</span><span class="n">dtype</span><span class="p">,</span> <span class="p">[</span><span class="n">subdtypes</span><span class="p">(</span><span class="n">dt</span><span class="p">)</span> <span class="k">for</span> <span class="n">dt</span> <span class="ow">in</span> <span class="n">subs</span><span class="p">]]</span>
<span class="gp">   .....:</span> 
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-576">所有numpy类型都是<code class="docutils literal"><span class="pre">numpy.generic</span></code>的子类：</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [387]: </span><span class="n">subdtypes</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">generic</span><span class="p">)</span>
<span class="gr">Out[387]: </span>
<span class="go">[numpy.generic,</span>
<span class="go"> [[numpy.number,</span>
<span class="go">   [[numpy.integer,</span>
<span class="go">     [[numpy.signedinteger,</span>
<span class="go">       [numpy.int8,</span>
<span class="go">        numpy.int16,</span>
<span class="go">        numpy.int32,</span>
<span class="go">        numpy.int64,</span>
<span class="go">        numpy.int64,</span>
<span class="go">        numpy.timedelta64]],</span>
<span class="go">      [numpy.unsignedinteger,</span>
<span class="go">       [numpy.uint8,</span>
<span class="go">        numpy.uint16,</span>
<span class="go">        numpy.uint32,</span>
<span class="go">        numpy.uint64,</span>
<span class="go">        numpy.uint64]]]],</span>
<span class="go">    [numpy.inexact,</span>
<span class="go">     [[numpy.floating,</span>
<span class="go">       [numpy.float16, numpy.float32, numpy.float64, numpy.float128]],</span>
<span class="go">      [numpy.complexfloating,</span>
<span class="go">       [numpy.complex64, numpy.complex128, numpy.complex256]]]]]],</span>
<span class="go">  [numpy.flexible,</span>
<span class="go">   [[numpy.character, [numpy.string_, numpy.unicode_]],</span>
<span class="go">    [numpy.void, [numpy.record]]]],</span>
<span class="go">  numpy.bool_,</span>
<span class="go">  numpy.datetime64,</span>
<span class="go">  numpy.object_]]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-577">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-578">Pandas还定义类型<code class="docutils literal"><span class="pre">category</span></code>和<code class="docutils literal"><span class="pre">datetime64 [ns，</span> <span class="pre">tz]</span></code> numpy层次结构，并且不会显示与上述函数。</span></p>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-579">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-580"><code class="docutils literal"><span class="pre">include</span></code>和<code class="docutils literal"><span class="pre">exclude</span></code>参数必须是非字符串序列。</span></p>
</div>
</div>
